Skip to content

Instantly share code, notes, and snippets.

@mingsai
Created May 13, 2020 17:20
Show Gist options
  • Save mingsai/0e1a4d733f4fe7a89c4428478bff0206 to your computer and use it in GitHub Desktop.
Save mingsai/0e1a4d733f4fe7a89c4428478bff0206 to your computer and use it in GitHub Desktop.
Awk - Split csv file by value and retain header
#retains header
awk -F, 'NR==1 {h=$0; next} {f=$3".csv"} !($3 in p) {p[$3]; print h > f} {print >> f}' input.csv
#option 2
HDR=$(head -1 input.csv); for fn in $(tail -n+2 input.csv | cut -f3 -d, | sort -u); do echo $HDR > $fn.csv; done
awk -F, '{print >> ($3".csv")}' input.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment