grep for a variable problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep for a variable problem
# 1  
Old 02-28-2006
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
# 2  
Old 02-28-2006
Try something like this:
Code:
count=$(/usr/bin/grep \<$itemno\> $BNFILE  | wc -l)
if ((count)) ; then
      echo yes
else
       echo no
fi

# 3  
Old 03-01-2006
Quote:
Originally Posted by Perderabo
Code:
count=$(/usr/bin/grep \<$itemno\> $BNFILE  | wc -l)

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

Code:
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.

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

# 4  
Old 03-01-2006
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
# 5  
Old 03-01-2006
Of course they are picking up < and > as part of the search string. What other behavior did you expect?
# 6  
Old 03-01-2006
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?
# 7  
Old 03-01-2006
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

2. Shell Programming and Scripting

grep variable

I've got a file that I'm trying to grep through that looks like this: alpha1 alpha2 alpha3 beta1 beta2 gamma5 gamma6 gamma7 gamma8 gamma9 and I want the output to only contain the line with the highest value for each, so the output I want is: alpha3 beta2 gamma9 I also need... (11 Replies)
Discussion started by: tiberione
11 Replies

3. Shell Programming and Scripting

grep in a variable

Hello, I usually search extensively and have to date found what I've needed. However, this one's got me stumped. I need to create a variable as follow. The issue however is that upon execution, it freezes. $var1 isn't always present in usage.log and this is fine but I'd like it to continue with... (6 Replies)
Discussion started by: shadyuk
6 Replies

4. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

5. Shell Programming and Scripting

Problem with a variable withing a variable

hello there, basically im screwed with a variable that should take the last modification date of a file. my code is fileCreationTime=$(( `ls -l $fileName | tr -s " " | cut -d " " -f6` )) my problem arise coz when the code is executed and stored in a file the return value is 1993 and not... (4 Replies)
Discussion started by: thurft
4 Replies

6. Shell Programming and Scripting

grep using variable

I have a pattern like: column "5" is missing PS: the no is in double quotes. The number usally changes, so we use a loop to grep. grep 'column "$number" is missing' filename.txt But it is not working.... How to solve this? (2 Replies)
Discussion started by: karumudi7
2 Replies

7. Shell Programming and Scripting

grep using variable

how can I use grep with a variable to find a value? cat data.out Hello World grep "Hello World" data.out Hello World ## Value found I want to do something like this but can't seem to get it to work any suggestions would be appreciated. var="Hello World" grep $var data.out (3 Replies)
Discussion started by: BeefStu
3 Replies

8. Shell Programming and Scripting

grep a variable

can i grep a variable say i have a variable var=`hostname` and I want to make an if statement like if grep "esp-ueh" $var;then...... how can i do this I dont want to store this variable in a file and the grep it because my script will be used at the same time on multiple stations and then that... (9 Replies)
Discussion started by: lassimanji
9 Replies

9. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

10. Shell Programming and Scripting

grep with variable

Hi, I can't get this script to work (returns 0, should return 3): $ cat A.lst | \ while read LINE do echo "$LINE" grep -c "$LINE" B.tmp done> > > > > Socket 0 $ but in contrast this one works fine (returns 3 as expected): $ LINE=Socket $ grep -c $LINE B.tmp 3 $ (5 Replies)
Discussion started by: ozvena
5 Replies
Login or Register to Ask a Question