1830997 Members
2484 Online
110018 Solutions
New Discussion

Re: script help

 
SOLVED
Go to solution
Ian James_1
Occasional Contributor

script help

Hi All

I have a quick query about a script I am trying to put together

I have a file full of address' I need to check wether the address' in the file resides in other files

I have put this together at the moment

for match in $(cat nomatch_add.dat)
do
newmatch=\'$match\'
grep $newmatch > /tmp/totals
if [ $? = 0 ]
then
num=$(cat /tmp/totals)
echo "$match exists in $num times"
fi
done

The problem I am having is with the newmatch. I thought that if I add ' to the start and end of the variable then grep would treat the variable as one entry to match.

echo $match
2 HARROW LANE

newmatch=\'$match\'

echo $newmatch
'2 HARROW LANE'

I get this though

grep $newmatch

grep: can't open HARROW
grep: can't open LANE
grep: can't open '

How can I resolve this

Thanks in advance

8 REPLIES 8
Con O'Kelly
Honored Contributor

Re: script help

Hi

Put "" around the grep string as follows:
grep "$newmatch"

At the moment grep is trying to find 2 in the filenames HARROW and LANE.

Cheers
Con
Steven E. Protter
Exalted Contributor

Re: script help

This might be a data problem. Looks like you are getting some characters you don't expect in your file, before the data you want to grep for. If there is a space in the file, thats interpreted by grep as the break between the first and second command line parameters.

Hence your error.

try this

grep steve man

you get

grep: can't open man

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Patrick Wallek
Honored Contributor
Solution

Re: script help

Try this instead:

#!/usr/bin/sh

while read match
do
grep "$match" > /tmp/totals
if [ $? = 0 ]
then
num=$(cat /tmp/totals)
echo "$match exists in $num times"
fi
done < nomatch_add.dat


If you want to shorten it even more:

while read match
do
num=$(grep "$match" | grep -v grep | wc -l)
echo "$match exists in $num times"
done < nomatch_add.dat
Ian James_1
Occasional Contributor

Re: script help

Thanks for the replies

Stephen,

Thats why I encased the 2 HARROW LANE in ' '

Con

I tried that but

echo "$newmatch"
'2 HARROW ROAD'

grep "$newmatch"

The above doesn't return anything, yet

tail -1

2 HARROW ROAD

As it is trying to match '2 HARROW ROAD'

I have therefore dropped the ' ' from the 2 HARROW ROAD

Gone back to $match and simply encased the $match with " "

grep "$match"

doh !!!!

fundamentals me thinks

Thanks for the replies



curt larson_1
Honored Contributor

Re: script help

even shorter then

while read match
do
num=$(grep "$match" | grep -v grep | wc -l)
echo "$match exists in $num times"
done < nomatch_add.dat

would be

while read match
do
num=$(grep -c "$match" )
echo "$match exists in $num times"
done < nomatch_add.dat
Ian James_1
Occasional Contributor

Re: script help

Hi

Seems' I have another issue

for match in `cat | head -1`
do
echo $match
2
HARROW
ROAD

uhh!!!!

cat | head -1
2 HARROW ROAD


Why is this ???


Thanks in advance
Ian James_1
Occasional Contributor

Re: script help

Ho Hum

Have ammended to

cat | while read match

Seems to have done the trick

Thanks everyone

curt larson_1
Honored Contributor

Re: script help

and maybe a bit too terse

file="nomatch_add.dat"
cat $file |
while read match
do
printf "$match exists in $file %d times\n" $(awk '/'"$match"'/ {t++;} END {print t;}' $file)
done