Script to separate file

Hi,
Could anyone help me with this please.

Input file --

ant 1 2 3 4
2 3 4 56 7
dog 8 9 56
ant 2 3 4 5
cvh 6 7 8
ant 1 3 45
78 0 - 

Would like to split the file as soon as it encounters the word "ant" very first time.

First Output file--

ant 1 2 3 4
2 3 4 56 7
dog 8 9 56

Second Output file--

ant 2 3 4 5
cvh 6 7 8

Third Output file--

ant 1 3 45
78 0 -

Thanks,

 csplit filename /ant/ {*}

produces a series of files named xx00 xx01 xx02
the /ant/ part is a regular expression.

See csplit --help for more options. I am assuming linux or GNU coreutils on your machine.

1 Like

Hello Indira2011, Could you please try following and let me know if this helps you.

 awk '{print > ($1=="ant"?++count:count)".txt"}'   Input_file OR awk '$1 == "ant"{count++} {print > count".txt"}'   Input_file 

Thanks, R. Singh