Last active
October 1, 2020 16:25
-
-
Save whyvez/5c7ca73e901cbdbde4ce72c9d41a3e96 to your computer and use it in GitHub Desktop.
Simple awk csv to ndjson converter.
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
BEGIN { | |
FPAT = "([^,]*)|(\"[^\"]+\")" | |
} | |
NR == 1 { | |
split($0, header, ",") | |
} | |
NR > 1 { | |
printf "%s", "{" | |
for (i = 1; i <= NF; i++) { | |
key=header[i] | |
val=$i | |
gsub("^\"+", "", val) # remove leading double quotes | |
gsub("\"+$", "", val) # remove trailing double quotes | |
gsub(/\\/, "\\\\", val) # json escape backslash | |
gsub(/"/, "\\\"", val) # json escape double quote | |
if (val !~ /^0+/ && (val ~ /^[0-9]+$/ || val ~ /^[0-9]*[.][0-9]*$/)) { | |
format = "\"%s\":%s" | |
} else { | |
format = "\"%s\":\"%s\"" | |
} | |
printf format, key, val | |
if (i != NF) printf "%s", "," | |
} | |
printf "%s\n", "}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment