Computing.Net > Forums > Unix > Split a Text file using blank line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Split a Text file using blank line

Reply to Message Icon

Name: nish_r
Date: July 30, 2006 at 23:57:45 Pacific
OS: Cygwin
CPU/Ram: 512
Comment:

Hello,

I have a text file in following format. Separate blocks of data are there, they are separated by a blank line. There are NO blanks in between data rows. First row of each data clock is a fixed one.


Game Date Process CDR Sell Reck
23 Test rts 123TST rt 45
34 No riverse 123 only na na
. . . . . .
155 na govt frame no sk


Game Date Save
12 no yes - do
13 Yes exact
. . .
16 do perfect

I need to split these data blocks in to different text files.

My output file 1 :

Game Date Process CDR Sell Reck
23 Test rts 123TST rt 45
34 No riverse 123 only na na
. . . . . .
155 na govt frame no sk

My output File 2:

Game Date Save
12 no yes - do
13 Yes exact
. . .
16 do perfect

Appreciate your assistance to do this process using a UNIX script.

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: July 31, 2006 at 01:44:04 Pacific
Reply:

Assuming the "blank lines" you described is fixed, eg "\n\n\n", and also your file is not very big, using Python:

>>> f = open("inputfile.txt").read()
>>> all = f.split("\n\n\n")
>>> i = 1 #counter to name your output files
>>> for items in all:
............open(str(i) + ".txt","w").write(items)
............i = i + 1
...
>>>

the output files will be 1.txt , 2.txt...etc



0

Response Number 2
Name: nails
Date: July 31, 2006 at 07:02:12 Pacific
Reply:

I think you want to use the csplit command which splits a file based on regular expressions. Take a look at these links:

http://www.computing.net/unix/wwwboard/forum/7415.html
http://www.computing.net/unix/wwwboard/forum/7134.html


0

Response Number 3
Name: lchi2000g
Date: July 31, 2006 at 07:34:15 Pacific
Reply:

csplit -zs -f item file.txt /^Game\ Date/ {*}


Luke Chi


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Split a Text file using blank line

Remove blanks from a text file www.computing.net/answers/unix/remove-blanks-from-a-text-file/6582.html

Print certain line from a text file www.computing.net/answers/unix/print-certain-line-from-a-text-file/6955.html

Parsing and emailing a text file www.computing.net/answers/unix/parsing-and-emailing-a-text-file-/6623.html