Created
August 26, 2012 17:22
-
-
Save drjerry/3481798 to your computer and use it in GitHub Desktop.
Converts stream of tabular records to stream of JSON records.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
if (NR == 1) { | |
split($0, tags); | |
if (EC == "") EC = "\""; | |
} | |
else { | |
split($0, vals); | |
jrec = "{"; | |
for (i = 1; i <= NF; ++i) { | |
if (vals[i] ~ /[^0-9.]/) | |
jrec = jrec EC tags[i] EC ":" EC vals[i] EC; | |
else | |
jrec = jrec EC tags[i] EC ":" vals[i]; | |
if (i < NF) | |
jrec = jrec ", "; | |
else | |
jrec = jrec "},"; | |
} | |
print jrec; | |
} | |
} |
the last comma problem is corrected by adding two lines to the script, seen here:
minkymorgan / tab2json.awk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great script,
One small bug, is it possible to remove the last comma on the last record?