
Extracting lines from a text file that match a certain criteria to another text file
Hi,
I am novice UNIX shell programmer and would like some help. I am
trying to write a shell script that will read a text file line by
line. The task is to extract lines from the text file that meet a set
of criteria to another text file.
An example of the text file, foo2, that I am using is shown below,
0,0,"NBSC",12,"24/02/2004 15:35:34","RD","2.2"
1,1,"ORG-UNIT-TYPE","C",1,"13/03/1997 17:24:49","PO"
1,2,"ORG-UNIT-TYPE","C",2,"13/03/1997 17:24:49","Client"
1,3,"ORG-UNIT-HIST","C",3,"13/03/1997 17:24:49","PON"
The task is to look at the 3rd field of each line, and extract the
whole line if the third field matches the criteria.
The criteria is to extract all lines that have the 3rd field set to
the value of "NBSC" to a separate text file.
Similarly to extract all lines, to another separate file, that have
the 3rd field set to the value of "ORG-UNIT-HIST".
All lines with 3rd field set to, "ORG-UNIT-TYPE" are to be ignored.
The psuedo code that I have come up with so far is shown below,
while read -r rec_type
do
NEXT=`echo ${rec_type} | cut -f 3 -d,`
if "${NEXT}" = "NBSC" then
write to text file1
else
if "${NEXT}" = "ORG-UNIT-HIST" then
write to text file 2
fi
fi
done < foo2
Any help or pointers would be greatly appreciated. Thanks in
antcipation