Last active
April 14, 2018 12:30
-
-
Save shokoe/c7d91fb0f7cec03494dc26d77fb3d939 to your computer and use it in GitHub Desktop.
Print csv file by headers
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
#!/bin/bash | |
# Syntax: csv_header.sh "<space delimited head names>" "<empty string replacment>" | |
awk -F, -v E="${2:--}" -v H="$1" -v D="^^^" ' | |
NR==1 { | |
split(H,h," ") | |
for (i=1; i<=NF; i++) { | |
f[$i] = i | |
} | |
for (i in h) { | |
printf "%s%s", h[i], D | |
} | |
print "" | |
} | |
NR!=1 { | |
for (i in h) { | |
#print i".."h[i]".."$(f[h[i]]) | |
if ( length(f[h[i]]) == 0 ) printf "%s%s", E, D | |
else printf "%s%s", $(f[h[i]]), D | |
} | |
print "" | |
} | |
' | column -t -s '^^^' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment