Skip to content

Instantly share code, notes, and snippets.

@cjlyth
Created October 18, 2013 22:58
Show Gist options
  • Save cjlyth/7049472 to your computer and use it in GitHub Desktop.
Save cjlyth/7049472 to your computer and use it in GitHub Desktop.
gawk script to flatten and read a javascript object, built for a small use case that involved reading a node.js package.json
#!/usr/bin/gawk -f
BEGIN {
RS="(\"*[[:alnum:]*]\"[ \t]*:[ \t]*)?\"*[ \t]*(,|{)+[ \t]*|\n[ \t]+\"|(\"[[:alnum:]*]\"[ \t]*:[ \t]*)?{|\"+[\n]+";
FS="\"[ \t]*:[ \t]*|\"?[ \t]*:[ \t]*\"";
column_format ="| %-39s";
cols = 0; rows = 0;
h_line="----------------------------------------------------------------------------------------";
header_format="%89s\n%-36sSimple JSON Parser%36s\n%89s\n";
footer_format="%89s\n|%44s%-44s|\n%89s\n";
printf header_format, h_line, "|", "|",h_line
}
NF > 1 {
for (i=1; i<=NF; i++) {
printf column_format, $i
};
cols = (NF > cols) ? NF : cols;
rows = rows + 1;
printf " |\n"
}
END {
footer_message=(cols " columns in " rows " rows ");
halfLength=length(footer_message)/2;
sOne=substr(footer_message,1, halfLength);
sTwo=substr(footer_message,halfLength+1, halfLength*2);
printf footer_format, h_line, sOne, sTwo ,h_line
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment