|
|
|
|
|
by 055static
4989 days ago
|
|
Wow, this compiles fast. And the Flex and Bison files are there. This seems hackable. Nice work! My only questions are (they are always the same with any purported sed/awk "replacement"): 1. what was the problem you were trying to solve where sed and awk failed you, and 2. does this program operate line by line or does it read entire files into memory? I had to deal with some JSON a while ago and threw together some sed like this just so I could read it: sed '
s/,/&\
/g;
/^$/d;
s/^[{][^}]/\
&/g;
/\"/s/,/<##eol##>/g;
s/ *//;
' |tr '\012' '\040' |sed '
s/<##eol##>/\
/g;
s/\[/\
&\
/;
s/\]/\
&\
/;
s/, /, /g;
'
But any difficuly I have dealing with JSON I attribute to the pervasive use of JSON, not sed. |
|
1. I think your example shows sed/awk's failings with JSON data :) I don't want to write a JSON parser by hand every time I want to pull a field out of an object, and parsing recursive structures with regexes is never a good plan.
2. It reads JSON items from stdin into memory, one at a time. So if the input is a single giant JSON value, it all goes into memory, but if it's a series of whitespace-separated values they'll be processed sequentially.
It's cat-friendly: if you do
then it's the same as doing