secret
Created

for Anuj@stackoverflow

  • Download Gist
split_anuj.bash
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 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
}
'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.