Skip to content

Instantly share code, notes, and snippets.

@shawnchin
Created September 21, 2012 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnchin/8b230d01ec09a3d1a898 to your computer and use it in GitHub Desktop.
Save shawnchin/8b230d01ec09a3d1a898 to your computer and use it in GitHub Desktop.
for Anuj@stackoverflow
# Related comments: http://stackoverflow.com/questions/12515584/splitting-large-file-into-small-multiple-files-based-on-the-column-concatenation#comment16849432_12515771
# Related chat: http://chat.stackoverflow.com/rooms/16930/discussion-between-shawn-chin-and-anuj
# the following splits a file into new files that are named based on fields 2, 3 and 5
# The first line of the input file is used as a header for all new files created
sort -k2,3 -k5,5 -t, infile.txt | awk -F, '
FNR == 1 { # if first line
header = $0; # store line as header
}
FNR != 1 { # NOT first line
filename = $2"_"$3"_"$5;
if (filename != prev) { # new label found
print header > filename # create new file with header
prev = filename # store
}
print >> filename # append current line to file
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment