Problem with splitting large file based on pattern

Hi Experts,

I have to split huge file based on the pattern to create smaller files. The pattern which is expected in the file is:

Master.....
First...
second....
second...
third..
third...
Master...
First..
second...
third...
Master...
First...
second..
second..
second..
third...
third..
so on...

In the above file total..i have three blocks starting with Master & ending with third which is considered as one block.

If i split this into two files:

My first file should have:

Master.....
First...
second....
second...
third..
third...
Master...
First..
second...
third...

And my second file should have:

Master...
First...
second..
second..
second..
third...
third..

I have written the below code which is providing me one block in each file but i want to write mulitiple blocks in each file.

awk '/^1/{f=1} f{ print $0 > "file_"n ; c++} c>10000 && /^3/ { n++; c=1; close("file_"n) }' c=1 n=1 testfile

Can you please provide any suggestion to me on how to achieve this?

Thanks,
SS

Try this...

awk 'BEGIN {blocks=b=5;i=1} /Master/{p=1} p{ print > i".log" } /third/{--p;if(!--b){++i;b=blocks}} ' infile

Assign the required blocks in one file in the BEGIN block.

--ahamed

Hi Ahamed,

I have modifed the command as shown below:

awk 'BEGIN {blocks=b=5;i=1} /Master/{p=1;p++} p{ print > i".log" } /third/{--p;if(!--b){++i;b=blocks}} ' infile

but this command is dropping the second occurance of third record set in the block.

Can you please look into this?

---------- Post updated at 01:50 AM ---------- Previous update was at 12:59 AM ----------

Hi,

I solved this by using below command. Thanks a lot for looking into this.

awk '/Master/{n++}{print >> test int((n+100)/100)}' filename

Thanks
SS