Split a file using awk command.

awk 'FNR == 1 { c = 1 } { print > (f c) } !FNR%n { close(f c); ++c }' n=$files_per_stream f=$input_path/filename_ $input_file

$input_file with some records are splitted into files named filename_1,filename_2...etc according to $files_per_stream.

Plz help me know how and if anyone has better option plz let me know ??

thnx in advance.

Perhaps try the split command.

1 Like

but how this awk command is working..
plz help me out.

thnx.

FNR == 1 { c = 1 } - For the first record in each input file, set c to 1.
{ print > (f c) } - For all records, write the current record to file (f and c concatenated, e.g. $input_path/filename_1)
!FNR%n { close(f c); ++c }' - if the current record number is exactly divisible by n, close the current output file and increment variable c.
n=$files_per_stream f=$input_path/filename_ $input_file - set variables & input file.

thanks CarloM but i still have some doubts...

1.) untill current record number is exactly divisible by n ,the filename_1 will be appended ????
This is an implicit behaviour of awk or what part of code code this ??

2.) what is "!" used for ?????

@guptam: I don't understand, did you find this code somewhere, how did you come across this? What are your requirements, what did you try yourself?

  1. Yes.
    Which behaviour do you mean? The second statement explicitly writes to the named file, and the third one explicitly checks FNR & changes the variable used to generate the filename.
  2. Not/negate - if FNR%n is FALSE or zero, !FNR%n is TRUE.

pardon me for inconvinience...

actually my operating system got crashed today...so i am unable to try myself...

hope i will soon get my system right and then i will share some some relevant questions.

thanks once again CarloM.