Last active
August 29, 2015 14:26
-
-
Save mkropat/1fdde16aafe3b769bd1b to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/sh | |
main() { | |
for f; do | |
dir=$(dirname -- "$f") | |
filename=$(basename -- "$f") | |
case "$filename" in | |
*.csv) outfilename=${filename%.*}.json ;; | |
*) outfilename=$filename ;; | |
esac | |
awk "$script" "$f" >|"$dir/$outfilename" | |
done | |
} | |
script=' | |
BEGIN { | |
FS="," | |
} | |
{ | |
if (FNR == 1) { | |
for (i = 0; i <= NF; i++) { | |
fields[i] = $(i) | |
} | |
} | |
else { | |
printf "{ " | |
sep="" | |
for (i = 1; i <= NF; i++) { | |
printf sep "\"" fields[i] "\":\"" $(i) "\"" | |
sep=", " | |
} | |
print " }" | |
} | |
}' | |
main "$@" |
This file contains 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
#!/bin/awk -f | |
BEGIN { | |
FS="," | |
} | |
{ | |
if (FNR == 1) { | |
for (i = 0; i <= NF; i++) { | |
fields[i] = $(i) | |
} | |
} | |
else { | |
printf "{ " | |
sep="" | |
for (i = 1; i <= NF; i++) { | |
printf sep "\"" fields[i] "\":\"" $(i) "\"" | |
sep=", " | |
} | |
print " }" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment