-
-
Save shawnchin/8b230d01ec09a3d1a898 to your computer and use it in GitHub Desktop.
for Anuj@stackoverflow
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
# 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