Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with script using grep if and plus sign 2

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi all,

I need another set of eyes and brains to explain why I can't get this working. If could be a simple case of bracket problems which always catch me out ;-)

I have a file called main.txt which I run the script against. The script is meant to check if certain lines of text appear in the file and perform a condition if they do. In this sample just a echo if good or bad really.

main.txt contains:
Code:
100 bbb ccc 0 eee fff ggg+ hhh iii
100 bbb ccc 1 eee fff ggg+ hhh iii
100 bbb ccc 2 yyy fff ggg+ hhh iii
100 bbb ccc 3 eee fff ggg+ hhh iii
100 bbb ccc 1 eee fff ggg+ hhh ddd
100 bbb ccc 5 eee fff ggg+ hhh iii
100 bbb ccc 12 eee fff ggg+ hhh iii
100 xxx ccc 1 eee fff ggg+
100 xxx ccc 5 eee fff ggg+
100 xxx ccc 12 eee fff ggg+

The data file contains the following lines of text which are the lines I need to look for:
Code:
Pass,100 bbb ccc 1 eee fff ggg+ hhh iii,all,on
Pass,100 xxx ccc 5 eee fff ggg+,all,on

This is what the script looks like:
Code:
#!/usr/bin/bash

DATA="data2.csv"

while read LINE
 do
  STD=`echo $LINE | awk -F, '{print $2}'`
  CHK=`grep -w "$STD" $1 `

echo "---------------- "
# Display if output is caught or not:
echo "STD = $STD"
echo "CHK = $CHK"
echo "---------------- "

   if [ "$CHK" = "$STD" ]
    then
     echo " *** GOOD - $STD found ***"
   elif [ "$CHK" = "" ]
    then
     echo " *** BAD - $STD not found ***"
   fi
 
 done < $DATA

This is the output when the script is run against it:
Code:
---------------- 
STD = 100 bbb ccc 1 eee fff ggg+ hhh iii
CHK = 100 bbb ccc 1 eee fff ggg+ hhh iii
---------------- 
 *** GOOD - 100 bbb ccc 1 eee fff ggg+ hhh iii found ***
---------------- 
STD = 100 xxx ccc 5 eee fff ggg+
CHK = 
---------------- 
 *** BAD - 100 xxx ccc 5 eee fff ggg+ not found ***

As you can see the output is not working too well... any idea why this is?
It seems my biggest headache might be because of the + sign, or with the way I'm doing the if checks...

Should I approach this in a total different way perhaps? I'm open to suggestions :)

Thanks

 
Hi

Well, my GNU [tt]grep[/tt] does not have that problem unless I specify -E. But try it :
Code:
STD=`echo $LINE | awk -F, '{[red]gsub(/\+/,"\\+");[/red]print $2}'`

Feherke.
 
The simplest way, for me, is to use grep -F (aka fgrep).

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks to you both Feherke and PHV ... I think you've solved my issue :) (I'll test further once I sort my locked ID hehe)

I'll use the fgrep command in my script, and will freeze the gsub routine as I can see another use for it me in another messy script of mine ;-)

Cheers... T
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top