Split file Dynamically

Hello ,

I have a flat file ( comma separated ) and want to split dynamically .

If I provide input 3 then rows 1,4,7 will o/p to a file and rows 2,5,8 will redirect to 2nd file and 3,6,9 rows will go to 3rd file

So 3 files will be generated .

Could it be possible in Unix?

Try:

awk '{f=NR%x?NR%x:x;print > "file"f}' x=3 infile
3 Likes

An alternative that does not require a conditional:

awk '{print > "file" ((NR-1)%x)}' x=3 infile

If a 1-index solution is preferred, just add one to the modulus, (NR-1)%x+1 .

Regards,
Alister

2 Likes