Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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 < ctlfileI 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 < ctlfilehead 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 < ctlfilecat outfile
line 8
line 1
line 2
line 3
line 9
line 10
line 4
line 5
line 6
line 7

![]() |
Automate scp from Unix to...
|
Finding idle time of a pr...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |