Why doesn't "grep -w" ALWAYS work?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why doesn't "grep -w" ALWAYS work?
# 8  
Old 02-27-2012
More details

All, I apolgize for not replying sooner, for some reason, I was not recieving notification when there were replies..

Here is some more info regarding a larger snippet of the script, with some comments that I hope make it easier to understand:
************************************************
Code:
for i in `cat $ROUTERS`
do
devname=""
shortname=""
location=""
make=""
model=""
iosver=""
ifname=""
ifalias=""
mem=""
sernum=""
devname=$i
shortname=`echo $devname | cut -d "." -f 1`
location=`/opt/OV/bin/runsql location.sql | grep $devname | cut -d "|" -f 2 | awk '{print $1}'`
make=`/opt/OV/bin/runsql oem.sql | grep $devname | cut -d "|" -f 2 | awk '{print $1}'`
model=`/opt/OV/bin/runsql details.sql | grep $devname | cut -d "|" -f 3 | awk '{print $1}'`
iosver=`/opt/OV/bin/runsql details.sql | grep $devname | awk -F"Version" '{print $2}' | cut -d , -f 1`
mem="UnderConst"
sernum=`grep -i $devname /opt/OV/contrib/NNM/CBTS/data/sn.csv | cut -d "," -f 2`
/opt/OV/bin/runsql node_ifs.sql | grep $devname | cut -d "|" -f 2,5,6 > $WORKDIR/tmp_$shortname.tmp        
for i in `cat $WORKDIR/tmp_$shortname.tmp | awk '{print $1}'` 
## i is the IP address is this for loop ##
## So every time I grep for $i for the variables below, if $i was not uniquely found, then the corresponding variables are failing ##
## Each file for $WORKDIR/tmp/$shortname.tmp is the output given in the original post ##  
      do
        ifname=`/usr/xpg4/bin/grep -w $i $WORKDIR/tmp_$shortname.tmp | cut -d "|" -f 2 | awk '{print $1}'`
        ifalias=`/usr/xpg4/bin/grep -w $i $WORKDIR/tmp_$shortname.tmp | cut -d "|" -f 3 | tr ";" "-"`
        if [ $ifname = "Pseudo" ];then
          echo "Pseudo interface ....checking"
          location=`/usr/sfw/bin/snmpwalk -v2c -c decns $devname sysLocation.0 | cut -d ":" -f 4 | awk '{print $1}'`
          make=`/usr/sfw/bin/snmpwalk -v2c -c decns $devname  sysDescr.0 | head -1 | cut -d ":" -f 4 | awk '{print $1}'`
          model=`/usr/sfw/bin/snmpwalk -v2c -c decns $devname sysDescr.0 | head -1 | cut -d ":" -f 4 | cut -d , -f 2`
          iosver=`/usr/sfw/bin/snmpwalk -v2c -c decns $devname sysDescr.0 | head -1 | cut -d ":" -f 4 | cut -d , -f 3 | awk '{print $2}'`
        fi
        echo "$devname;$i;$ifname;$ifalias;$make;$model;$iosver;$location;$mem;$sernum" >> $REPORT
        #rm $WORKDIR/tmp_$shortname.tmp
        done
done

*****************************************************

thx,
SteveT

Last edited by jim mcnamara; 02-27-2012 at 11:09 AM.. Reason: please use code tags so we can read your code
# 9  
Old 02-27-2012
That's umpteen useless uses of cat and dangerous uses of backticks and 976 useless uses of awk | grep | cut | sed | kitchen | sink.

Whenever you have
Code:
for X in `cat Y`

you should be doing

Code:
while read X
do
...
done < Y

This has the useful side-effect of being able to split columns on its own, without needing cut or awk.
Code:
while read COL1 COL2 COL3 COL4
do
...
done < Y

Unfortunately two lines of comments do not clear up 12 different n-long pipe chains.
# 10  
Old 03-05-2012
When you get past the putdowns and sarcasm, Corona688 makes a couple useful points. Thank you for that. I'll try your suggestions in this and future scripts to avoid the pitfalls you mention.

However 2 examples of While read don't actually address the question of why grep -w gives the results it does.
# 11  
Old 03-05-2012
Quote:
Originally Posted by turk22
All, I apolgize for not replying sooner, for some reason, I was not recieving notification when there were replies..
Hi Steve, you can change the Default Thread Subscription Mode in your user control panel options..

S.
# 12  
Old 03-05-2012
Quote:
Originally Posted by turk22
When you get past the putdowns and sarcasm, Corona688 makes a couple useful points.
No putdowns contained, and no sarcasm given. You've simply hit a pitfall so common it has a form-letter.
Quote:
However 2 examples of While read don't actually address the question of why grep -w gives the results it does.
I suspect that it does, actually. for X in `cat file` has bad side-effects including (but not limited to) splitting strings wherever it feels like it, not just at newlines. Meaning, I'm suspicious that you're not grepping what you actually think you are.
# 13  
Old 03-05-2012
You can also try combining the two options:
Code:
grep -wF

As others have posted, you need to make the . literal otherwise the IP address may mismatch (. means any character in a regex context). The additional -w option ensures that partial literal matches are excluded...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, :) I've an issue with the creation of a directory, All work without it :mad: So, below, my scripts with the debug output : #!/bin/bash # PATHS HOME_BACKUP="/home/backup" HOME_SCRIPT="/home/scripts/test/backup_server" TARGET="/var/www" # DATE DATE_Ymd=$(date +%Y-%m-%d) #... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

2. AIX

AIX 5.3 on p275 - "|" key doesn't work (!)

More issues. The "|" key doesn't work at all, either in the text terminal or in mwm (which starts fine when I do "startx"). How am I supposed to troubleshoot without a "|" key? (2 Replies)
Discussion started by: smithfarm
2 Replies

3. AIX

"/" doesn't work on command prompt for searching commands last typed

When I use "/" to look for a particular command that I typed in the current session it says D02:-/home/user1/temp> /job ksh: /job: not found. D02:-/home/user1/temp> previously it used to fetch all the commands which had job in it.. for example subjob, endjob, joblist etc... may I... (7 Replies)
Discussion started by: meetzap
7 Replies

4. UNIX for Dummies Questions & Answers

Help! "grep" doesn't work for me!

totally pis*ed off. I have a data set (xxx.txt), as follows: chr1 3821 rs127372 A/C 0.823 chr1 3822 rs127376 A/C/G 0.899 chr1 3722 rs612634 A/C 9.22 chr1 3262 rs7152 A/T 0.22 chr1 3711 rs737 A/C/G 0.2323 ....... I only want to get those lines... (6 Replies)
Discussion started by: kaixinsjtu
6 Replies

5. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

6. UNIX for Advanced & Expert Users

sometimes "ps -elf" command doesn't work

when i give "ps -elf" or "ps" system gets hung. if i press "^c" come out from it... pls help..what should i do to get it resolved. thanks CKanth (4 Replies)
Discussion started by: srikanthus2002
4 Replies

7. Linux

By angle-brackets/"pipe" button doesn't work?

How can I configure it? I have a swedish keyboard with swedish keyboard setting. Everything works perfectly (едц) except that button. What can be wrong? /Richard ++ NOTE: It seems like the computer notices the input but that the button isn't assigned to anything (the keyboard-cursor stops).... (1 Reply)
Discussion started by: riwa
1 Replies

8. UNIX for Dummies Questions & Answers

Why "@z=$x+$y" doesn't work?

253 lab2-36:~/try_direct/another> set x=1;set y=2;@z=$x+$y @z=1+2: Command not found. 254 lab2-36:~/try_direct/another> (11 Replies)
Discussion started by: endeavour1985
11 Replies

9. Shell Programming and Scripting

my "case" doesn't work !

I'm using the case statement in the following script and it always takes the "*" default choice while it should be "3". I wonder why ??? dt_auj=`date +%d` NBLOG=`ls -al /users/notes01/LOG/t*|awk '{print $7}'|grep $dt_auj|wc -l` case $NBLOG in 1) cat ~/LOG/console-notes > $fic_tmp1 ;; 2)... (5 Replies)
Discussion started by: Nicol
5 Replies
Login or Register to Ask a Question