Split line to multiple files Awk/Sed/Shell Script help

Hi,

I need help to split lines from a file into multiple files.

my input look like this:

13
23 45 45 6 7
33 44 55 66 7

13
34 5 6 7 87
45 7 8 8 9

13
44 55 66 77 8
44 66 88 99 6

I want to split every 3 lines from this file to be written to individual files.

Please help me. Thank you in advance.

Hi

split -l4 infile outfile

4 is used since you have shown a blank line. if no blank line, use 3.

Guru.

1 Like

Thanks Guru, can you tell me how to get the output files names as 1.out, 2.out and so on.

Hi

Using split, it will generate files with aa,ab,ac and so on, and you have to manually rename it.

Use awk in this case which will give your expected:

awk ' {print > (NR%4?i:i++)".out"; }' i=1 infile

Guru.