Multiple File creation from Single file

I've one large file with below content

TD:    0001                   xxxx
thnb
..........
TD:    0001                   yyyy
abcd
.........
.......

TD:    0002                   xxyy
efgh.
...................
...................
TD:    0003                   xxxx
xyvx
................

I want to split the files based on TD: 000n content
So the 1st file will be

TD:    0001                   xxxx
thnb
..........
TD:    0001                   yyyy
abcd
.........
.......

2nd file will be

TD:    0002                   xxyy
efgh.
...................
...................

Any idea how can I achieve this?

Thanks!

Try:

perl -n0e 'while (/TD.*?(?=TD|$)/sg){$x=$&;$x=~/TD:\s*(\w+)/;open $f,">>","$1";print $f $x; close $f}' file
1 Like

Or maybe something like:

awk '/^TD/{close(f);f=$2}{print >> f}' file
1 Like

Hello Franklin52 & Bartus11,
Thanks a lot , your solution has worked perfectly.
Now I have one small problem with another file of similar patern, in my large file in place of "TD:" if I have "RC No./Name :" , ie

RC No./Name :       0001
........
..............
RC No./Name :       0001
........

RC No./Name :       0002
..........
RC No./Name :       0003

Then above solution is not working if I replace TD with RC.
Any idea why this is not working?

WHAT is not working? I guess it is writing everything to the same file named "No./Name"? Try f=$4 in Franklin52's proposal.

Hello Rudic,
I'm getting below error after following the suggestion

awk '/^RC No/{close(f);f=$4}{print >> f}' test.txt
awk: (FILENAME=test.txt FNR=1) fatal: expression for `>>' redirection has null string value

 awk '/^RC/{close(f);f=$4}{print >> f}' test.txt
awk: (FILENAME=test.txt FNR=1) fatal: expression for `>>' redirection has null string value

In this case you could try:

awk '/^RC No/{close(f);f=$NF}{print >> f}' test.txt

Hello Fraklin52,
Still getting same error :frowning:

awk '/^RC No/{close(f);f=$NF}{print >> f}' test.txt
awk: (FILENAME=test.txt FNR=1) fatal: expression for `>>' redirection has null string value

Then, has the very first line in your input file that "RC No" pattern in it?

Hello Rudic,
Yes, file has "RC No" in very first line of the file.

Please provide a realistic example of your input file.

Below are the realistic contect of my original file:

  RC No./Name :       0001     /Kosba office branch                        Dhapa Advertised Match PCL.
  Report Name  : SEZ223 DAILY EXPENSE ALLOCATE TO OTHER                                                  Page     : 1 of 8
  As of        : 31-JAN-14                                                                                 Run Date : 04 APR 2014
                                                                                                           Run Time : 18:41:11
  RC No./Name :       0001     /Kosba office branch
  Report Name  : SEZ223 DAILY EXPENSE ALLOCATE TO OTHER                                                  Page     : 6 of 8
  As of        : 31-JAN-14                                                                                 Run Date : 04 APR 2014

  RC No./Name :       0001     /Kosba office branch
  Report Name  : SEZ223 DAILY EXPENSE ALLOCATE TO OTHER                                                  Page     : 8 of 8
  As of        : 31-JAN-14                                                                                 Run Date : 04 APR 2014
                                                                                                           Run Time : 18:41:11
  RC No./Name :       0002     /Rathtala branch                              Dhapa Advertised Match PCL.
  Report Name  : SEZ223 DAILY EXPENSE ALLOCATE TO OTHER                                                  Page     : 1 of 2
  As of        : 31-JAN-14                                                                                 Run Date : 04 APR 2014
                                                                                                           Run Time : 18:41:11
  RC No./Name :       0002     /Rathtala branch
  Report Name  : SEZ223 DAILY EXPENSE ALLOCATE TO OTHER                                                  Page     : 2 of 2
  As of        : 31-JAN-14                                                                                 Run Date : 04 APR 2014
                                                                                                           Run Time : 18:41:11
   Total RC :                                                        0.00                 120.00

  RC No./Name :       0003     /Bang lampu branch                            Dhapa Advertised Match PCL.
  Report Name  : SEZ223 DAILY EXPENSE ALLOCATE TO OTHER                                                  Page     : 1 of 2
  As of        : 31-JAN-14                                                                                 Run Date : 04 APR 2014
   Total RC :                                                        0.00                  60.00
   : 31-JAN-14
awk '/Total RC/{next}/RC No/{close(f);f=$4}{print >> f}' test.txt

Hello Franklin52,
Thanks for your help. Now with new command last line i.e "Total RC" is not coming in any files.

Something like this?

awk '/RC No/{close(f);f=$4}{print >> f}' test.txt
1 Like

Hello Franklin52,
It worked perfectly! Thanks a lot!
btw, one small doubt
what is the difference between

awk '/RC No/{close(f);f=$4}{print >> f}' test.txt

and

awk '/^RC No/{close(f);f=$NF}{print >> f}' test.txt

/^RC No/ - Matches the pattern at the beginning of the line
NF means the number of fields and $NF means the last field

1 Like

Hello Franklin52,
Many Thanks!!!!