how to split a large csv file in unix command line how to split a large csv file in unix command line unix unix

how to split a large csv file in unix command line


Just close the files after writing:

awk -F, '{print > $2; close($2)}' test1.csv


You must have a lot of lines. Are you sure that the second row repeats enough to put those records into an individual file? Anyway, awk is holding the files open until the end. You'll need a process that can close the file handles when not in use.

Perl to the rescue. Again.

#!perl    while( <> ) {    @content = split /,/, $_;    open ( OUT, ">> $content[1]") or die "whoops: $!";    print OUT $_;    close OUT;}

usage: script.pl your_monster_file.csv

outputs the entire line into a file named the same as the value of the second CSV column in the current directory, assuming no quoted fields etc.