Skip to content

Instantly share code, notes, and snippets.

@whyvez
Last active October 1, 2020 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whyvez/5c7ca73e901cbdbde4ce72c9d41a3e96 to your computer and use it in GitHub Desktop.
Save whyvez/5c7ca73e901cbdbde4ce72c9d41a3e96 to your computer and use it in GitHub Desktop.
Simple awk csv to ndjson converter.
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