novicesmml
Posts: 1
Joined: Sun Sep 23, 2018 6:02 am

Split File

Sun Sep 23, 2018 6:23 am

I would like to split a file every time there is a blank line in the file but I am having some trouble. Here us an example of what I mean:
Original file:

blabla1
hi=24
bye=48

blabla2
hi=30
bye=6

blabla1
hi=90
bye=22

The output should be:
file number one named 24
should contain:

blabla1
hi=24
bye=48
file number two named 30
should contain:

blabla2
hi=30
bye=60
file number three named 90
should contain:

blabla2
hi=90
bye=22

The code I have right now is:
gawk '!/^$/{match($0, /^\hi= (.+?)/, k)} {print >k[1]".txt" }' sourcefilename
The issue is that the output is the new files with only the line hi=__ not the whole paragraph with all three entires.
Any help would be much appreciated!

I am trying to purpose this this answer I found online: https://unix.stackexchange.com/question ... text-files

tpyo kingg
Posts: 321
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Split File

Tue Sep 25, 2018 4:07 am

novicesmml wrote:
Sun Sep 23, 2018 6:23 am
I would like to split a file every time there is a blank line in the file ...
With the sample input and output provided, I would say to set the Record Separator pattern (RS) to be a span of two or more whitespace characters. The Field Separator pattern (FS) could be explicitly set to be a single whitespace character. Probably the Output Field Separator and Output Record Separator can be left untouched.

Then you could use a pair of match() functions. The first could extract everything from hi= to the end of the record to a variable, ignoring the individual fields by working on $0. The second would be to work on that variable to extract everything from the first span of digits to the end of the variable. Then clean up that variable by using sub() to clear everything after the digits. That variable will then contain the number you can use in the file name.

Perl is good for this too.

Return to “General programming discussion”