File Seperation in Unix

March 2, 2009 at 19:33:08
Specs: Windows XP
Hi,

I have a file like this.:
Command: vi myfile
Utility
Days of run:14thand28th
01/14/2009
01/28/2009
02/14/2009
02/28/2009
Utility
Days of run:2nd
01/02/2009
02/02/2009
03/02/2009
04/02/2009
Utility
Days of run:3rd
01/03/2009
02/03/2009
03/03/2009
04/03/2009

I want to seperate the above file(myfile) into three parts. Whenever the file reached "Days of run" string in the file it would create a new file with the contents upto the next occurence of the "Days of run".

For eg: I want the output as below files:
File Name:14thand28th
01/14/2009
01/28/2009
02/14/2009
02/28/2009
Utility
File name:2nd
01/02/2009
02/02/2009
03/02/2009
04/02/2009
Utility
Filename:3rd
01/03/2009
02/03/2009
03/03/2009
04/03/2009
Utility..

Can someone help me in this?. I am using Korn Shell in Unix.


See More: File Seperation in Unix

Report •


#1
March 2, 2009 at 23:12:30
Check out the unix csplit command:

#!/bin/ksh

csplit -ks -f filename datafile '/^Days/' {1000} 2> /dev/null

The above command splits datafile at the string starting with the word: Days.

The example uses a base of filename and starts with filename00, filename01, etc.

This command is similar to the unix split command that is number of lines based.


Report •

#2
March 3, 2009 at 05:20:57
Hi Nails,

The command worked as expected. Thanks for your quick reply!!
It really helped me lot.


Report •

Related Solutions


Ask Question