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

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.

Extracting lines from file using sh

Reply to Message Icon

Name: bengalliboy
Date: January 2, 2007 at 14:00:52 Pacific
OS: UNIX
CPU/Ram: 500
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: January 11, 2007 at 13:01:52 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More


Automate scp from Unix to... Finding idle time of a pr...



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

Extracting data from file using sed www.computing.net/answers/unix/extracting-data-from-file-using-sed/7660.html

Process line from file -Cshell scr www.computing.net/answers/unix/process-line-from-file-cshell-scr/5072.html

extract a specific line from file www.computing.net/answers/unix/extract-a-specific-line-from-file/5381.html