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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to grep a whole string, with spaces and dots etc

Status
Not open for further replies.

Autosys

Programmer
Joined
Jun 1, 2004
Messages
90
Location
GB
Hi All ...

My Script looks like this:

********************************************************
cat sanity.cfg | while read line
do

grep $line bigfile

if [ "$?" -ne "1" ]
then

echo "Cannot find $line"

fi

done;
**********************************************************


And the sanity.cfg file will have many strings in it that look like the one below:

<remote host="1.1.1.1" port="6018"/>

I need to check if these strings exist in a bigger file called bigfile, and if they don't raise a warning. The problem is that grep seems to complain about the special characters, dots and spaces in my string and break it up ... although when I do a "echo $line" I get the correct/whole line back. I tried grep -x & grep -w etc but they don't appear to work, also grep -E. I also tried putting the strings I'm searching for in the cfg file in single quotes .... Any ideas or advice or a possible better way of doing it would be fantastic .. Thanks a lot!

S

 
You may try something like this:
while read line
do
grep -F "$line" bigfile || echo "Cannot find '$line'"
done < sanity.cfg

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Use [tt]fgrep[/tt]. It does no pattern matching, so you can look for any regex characters without them being treated as special characters.
Code:
#!/bin/ksh

while read LINE
do
    fgrep "${LINE}" bigfile || print "Cannot find ${LINE}"
done < sanity.cfg
[tt]fgrep[/tt] is also usually a little faster since the pattern match is simpler.
 
SamBones, fgrep is the deprecated version of the grep -F suggestion i've made ...
 
Thanks a lot! Your posts helped me to finish my little script! Maximum Appreciation!

S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top