Skip to content

Instantly share code, notes, and snippets.

@shokoe
Last active April 14, 2018 12:30
Show Gist options
  • Save shokoe/c7d91fb0f7cec03494dc26d77fb3d939 to your computer and use it in GitHub Desktop.
Save shokoe/c7d91fb0f7cec03494dc26d77fb3d939 to your computer and use it in GitHub Desktop.
Print csv file by headers
#!/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