grep for a variable problem

I am trying to use two files and walk through the first one to see if a value from each record resides in the other file.

I am reading the file record by record and awking out the first field into a varaiable. That is working fine.

When I try to write my grep command it gives me an error.

cat $DSFILE | while read a
do
itemno=`echo $a | awk -F"|" '{print $1}'`
echo $itemno
if [ /usr/bin/grep \<$itemno\> $BNFILE ] ; then
echo "yes"
else
echo "no"
fi
done

When I run it I get:

--AA--06GR-741Z
+ [ /usr/bin/grep <--AA--06GR-741Z> /export/home/abc.before ]
./DS.skps.ksh[11]: <--AA--06GR-741Z>: unknown test operator
+ echo no

I tried the -w option too and it doesn't give me a result.

I must be having a brain cramp because I know I've done this somehow before.

thanks

Try something like this:

count=$(/usr/bin/grep \<$itemno\> $BNFILE  | wc -l)
if ((count)) ; then
      echo yes
else
       echo no
fi

If you have a -c option for your grep, then it becomes

count=$(/usr/bin/grep -c \<$itemno\> $BNFILE )

Or better yet, use the exit status of grep to find out if there was a match or not.

/usr/bin/grep -q \<$itemno\> $BNFILE
if [[ $? == 0 ]] ; then
   echo yes
else
   echo no
fi ;

My statements appear to be picking up the < ad> as part of the search string.

I have

count=$(/usr/bin/grep -c \<$itemno\> $BNFILE)
if (($count)) ; then
echo yes
else
echo no
fi

I have set -x on so I can see the statements and I get
I know this value exists in the file that I'm searching against.

+ read a
+ + awk -F| {print $1}
+ echo --AA--06BR-228Z|D
itemno=--AA--06BR-228Z
+ echo --AA--06BR-228Z
--AA--06BR-228Z
+ sleep 2
+ + /usr/bin/grep -c <--AA--06BR-228Z> /export/home/abc.before
count=0
+ (( 0 ))
+ echo no
no
+ sleep 2
+ read a

Of course they are picking up < and > as part of the search string. What other behavior did you expect?

This is the example I was given by another programmer to make the grep command pickup my variable as an absolute value since using quotes did not seem to work.

The grep command is interpreting the initial dashes in the data value as grep options.

So .. I guess my REAL question is.. how do I get the grep command to view my value which has dashes in the lead spaces and not interpret it as dashes for more options?

The easiest way is to use a modern grep. On Suns this is /usr/xpg4/bin/grep. Then anything following a -e is a search string, even if it has a leading -. Your use of /usr/bin/grep suggests that might want to use the old grep. For that, you will need to transform the string into a regular expression that matches the string but does not start with a -. Changing the leading - to [-] is the easiest way to do that.

PERFECT!
We recently moved to Sun and I wasn't aware there was a newer grep. Time to update my manuals.

Thanks!

+ read a
+ cat /export/home/ABC.EXP
+ + awk -F| {print $1}
+ echo --AA--06BR-001Z|D
itemno=--AA--06BR-001Z
+ echo --AA--06BR-001Z
--AA--06BR-001Z
+ sleep 2
+ + /usr/xpg4/bin/grep -e --AA--06BR-001Z /export/home/abc.before
+ awk {print $1}
+ wc
count=1
+ (( 1 ))
+ echo yes
yes
+ sleep 2