It is currently Mon, 27 Jun 2022 11:23:39 GMT



 
Author Message
 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



 Mon, 21 Aug 2006 18:09:34 GMT   
 Extracting lines from a text file that match a certain criteria to another text file

    If there are no commas within quotes, the task is not hard. The
    easiest way would be to use awk:

awk -F, '
   $3 == "\"NBSC\"" { print > "NBSC-file" }
   $3 == "\"ORG-UNIT-HIST\""  { print > "ORG-UNIT-HIST-file" }
' foo2

    If you really want to use a while loop:

exec 3> ORG-UNIT-HIST-filex
exec 4> NBSC-filex
IFS=,
while IFS= read -r line
do
   set -- $line
   case $3 in
      \"NBSC\") echo "$line" >&4 ;;
      \"ORG-UNIT-HIST\")  echo "$line" >&3 ;;
   esac
done < foo2

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



 Mon, 21 Aug 2006 18:30:42 GMT   
 Extracting lines from a text file that match a certain criteria to another text file
On 4 Mar 2004 02:09:34 -0800, mpate...@csc.com (M. Patel) wrote:

You're almost there, why did you stop?
If you're troubled with writing to files, try echo with append, i.e.

if [ "${NEXT} = \"NBSC\" ]
    then echo ${rec_type} >> file1

--
A. Alper Atycy
PGP key @ http://pks.gpg.cz:11371/pks/lookup?op=index&search=0xB824F550



 Tue, 22 Aug 2006 05:26:35 GMT   
 
   [ 3 post ] 

Similar Threads

1. Find First Match Using SED and Then Extract Text from Line

2. Script to extract portions of text from a text file

3. Urgent, need script or command to extract lines from a huge text file

4. Extracting lines from inside a text file (question for sed expert)

5. Extract different lines from a text file

6. Script to insert text before a certain line

7. Extracting lines of text

8. grep: printing only matched text, not whole line

9. How to extract a number from a line of text

10. Dos Text Files to Linux Text Files


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software