Computing.Net > Forums > Unix > Extracting lines from file using sh

Extracting lines from file using sh

Reply to Message Icon

Original Message
Name: bengalliboy
Date: January 2, 2007 at 14:00:52 Pacific
Subject: Extracting lines from file using sh
OS: UNIX
CPU/Ram: 500
Model/Manufacturer: Solaris
Comment:

Greetings,
I need to extract a file based on a control file that I am reading and create an output file. My control file has 3 char and rows may look like : ABC
DEF
GHI
The file I am extracting that is my source file has those lebels as follows:
#ABC#
line1
line2
#DEF#
line3
-----
Based on what I have on my control file, I will need to either extract only my needed lines for the assinged control or just comment out all other lines except that has a label on the control.
What is the easiest way to do this?
Many thanks


Report Offensive Message For Removal

Response Number 1
Name: James Boothe
Date: January 11, 2007 at 13:01:52 Pacific
Subject: Extracting lines from file using sh
Reply: (edit)

So, each line in your control file specifies a block of lines to extract from your data file?

cat infile
#ABC#
line 1
line 2
line 3
#DEF#
line 4
line 5
line 6
line 7
#GHI#
line 8
#JKL#
line 9
line 10
#ZZZ#

cat ctlfile
GHI
ABC
JKL
DEF


For each line in the control file, the following script will extract the referenced block of lines into a separate file.  Each output file will be named out.???

while read ctlword
do
sed -n "/#$ctlword#/,/#...#/p" infile > "out.$ctlword"
done < ctlfile

I will use the head command to display the out files ...

head out*
==> out.ABC <==
#ABC#
line 1
line 2
line 3
#DEF#

==> out.DEF <==
#DEF#
line 4
line 5
line 6
line 7
#GHI#

==> out.GHI <==
#GHI#
line 8
#JKL#

==> out.JKL <==
#JKL#
line 9
line 10
#ZZZ#


A problem with extracting from/to ranges is that the triggering from and to lines are included.  The following script has additional sed commands to weed out the triggering lines:

while read ctlword
do
sed -ne "/#$ctlword#/,/#...#/ba" -e b -e :a -e /#...#/!p infile > "out.$ctlword"
done < ctlfile

head out*
==> out.ABC <==
line 1
line 2
line 3

==> out.DEF <==
line 4
line 5
line 6
line 7

==> out.GHI <==
line 8

==> out.JKL <==
line 9
line 10


But you probably want all of the extracted lines in a single file, so the following version stacks all output into one file.

rm outfile 2> /dev/null
while read ctlword
do
sed -ne "/#$ctlword#/,/#...#/ba" -e b -e :a -e /#...#/!p infile >> outfile
done < ctlfile

cat outfile
line 8
line 1
line 2
line 3
line 9
line 10
line 4
line 5
line 6
line 7


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: Extracting lines from file using sh

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software