Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Split a Text file using blank line

Original Message
Name: nish_r
Date: July 30, 2006 at 23:57:45 Pacific
Subject: Split a Text file using blank line
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.


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: July 31, 2006 at 01:44:04 Pacific
Subject: Split a Text file using blank line
Reply: (edit)
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



Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: July 31, 2006 at 07:02:12 Pacific
Subject: Split a Text file using blank line
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: lchi2000g
Date: July 31, 2006 at 07:34:15 Pacific
Subject: Split a Text file using blank line
Reply: (edit)
csplit -zs -f item file.txt /^Game\ Date/ {*}


Luke Chi


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Split a Text file using blank line

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC