Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract portion of file on command line?

Status
Not open for further replies.

new2unix

Programmer
Feb 5, 2001
143
US
Hi,

I have a data file with the contents separated into different portion by date stamp header. Each date header line would be followed by multiple lines representing actual data. The date header is in the format of 11-Feb-2002, etc. Short of writing a script, is there a way to extract the portion of the file, say, between 11-Feb-2002 and 12-Feb-2002 to another file from the command line? I am using AIX.

Thanks for any suggestion

Mike
 
Another of my quick & dirty scripts here ... lets say you have an input file in the format
Code:
12-Feb-2002
start1
data1
data1
data1
data1
data2
end1
13-Feb-2002
start2
data2
data2
data2
data2
data2
data2
data2
data2
data2
end2
14-Feb-2002
start3
data3data3
data3
data3
data3
data3
data3
data3
end3
And you have the following script (I'll call it cutfile), which accepts 3 parameters - 1st date, 2nd date, and input filename
Code:
#!/bin/ksh
# cutfile script
D1=$1
D2=$2
FNAME=dfile

L1=$(grep -n $D1 $FNAME|cut -d: -f1)
L2=$(grep -n $D2 $FNAME|cut -d: -f1)

head -$(($L2 - 1)) $FNAME | tail -$(($L2 - $L1))
Run this script using cutfile 12-Feb-2002 13-Feb-2002 inputfile

I've thrown this together very quickly, but it may do the trick.

Greg.
 
Here is a quickie Sed command to do it:

sed -n '/12-Feb-2002/,/13-Feb-2002/p'

Kelly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top