Created
May 13, 2020 17:20
-
-
Save mingsai/0e1a4d733f4fe7a89c4428478bff0206 to your computer and use it in GitHub Desktop.
Awk - Split csv file by value and retain header
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
#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