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?
# 1  
Old 02-16-2012
Why doesn't "grep -w" ALWAYS work?

Working with IP addresses is a pain...

Here's my problem: I'm putting some interface information to a file:
Code:
 3.185.201.2     | Tu1102                  
 152.162.54.130  | Gi0/2.36                
 3.185.226.2     | Gi0/1                   
 3.185.0.82      | Tu1                     
 3.185.240.158   | Gi4/0                   
 207.22.157.43   | Lo1102                  
 3.185.220.2     | Lo0                     
 207.22.157.42   | Lo1101                  
 3.185.2.1       | Gi0/3                   
 3.185.200.2     | Tu100

then for each unique IP in this file, I'm gathering some information, so I do this:
Code:
/usr/xpg4/bin/grep -w $ipaddr tmp_$shortname.tmp

for 99% of my script it works fine, but using the example above, I get this:
Code:
 /usr/xpg4/bin/grep -w 3.185.2.1 tmp_$shortname.tmp     
 3.185.201.2     | Tu1102                  
 3.185.2.1       | Gi0/3

How is it that this first line is matching? It just makes no sense. I'm in ksh btw.

I appreciate any help that anyone can offer

Turk

Last edited by methyl; 02-16-2012 at 05:50 PM.. Reason: please use code tags
# 2  
Old 02-16-2012
This is because the dot is interpreted instead of being taken as litteral
To ensur the dot is taken as litteral, you must escape it by adding a backslash before it (or more if one is not sufficient)
give a try to this :
Code:
/usr/xpg4/bin/grep -w 3\.185\.2\.1 tmp_$shortname.tmp

# 3  
Old 02-16-2012
If you are trying to match a literal string, save yourself the hassle of escaping metacharacters and don't use regular expressions at all. See grep's -F option.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 02-20-2012
Thanks all,
I'm finding it difficult to escape the "." as I'm assigning the IP address to a variable. I guess it is possible to cut up the IP address and do it that way...

I did try the -F parameter, and got some interesting results. In the example above, it worked correctly , and only returned the desired IP address, but on others in the list, I got results where multiple lines were returned, but those were typically when I have .2 and a .22 or something along those lines, where the found string is correct, but part of a larger string, that i don't want.

What I found interesting is that out of close to 7000 IP addresses, only the example above was parsed incorrectly.

Thx, SteveT
# 5  
Old 02-20-2012
Quote:
/usr/xpg4/bin/grep -w $ipaddr tmp_$shortname.tmp
Your data as posted has a leading space character before the IP Address and at least one trailing space character after the IP Address.
You could include the space characters in the string.
Code:
/usr/xpg4/bin/grep -w " $ipaddr " tmp_$shortname.tmp

This technique works best with "grep -F" because there is no chance of some weird pattern match.
Code:
/usr/xpg4/bin/grep -F " $ipaddr " tmp_$shortname.tmp

# 6  
Old 02-20-2012
grep

Hi,
inp is like below,
Code:
10.2.10.22 dmjt
10.2.10.2 ttw

if we want to grep the second one, we have to use the below code,
Code:
grep '10.2.10.2 ' file

just add the space at the end of ip variable.
Cheers,
Ranga:-)
# 7  
Old 02-20-2012
Quote:
Originally Posted by turk22
I did try the -F parameter, and got some interesting results. In the example above, it worked correctly , and only returned the desired IP address, but on others in the list, I got results where multiple lines were returned, but those were typically when I have .2 and a .22 or something along those lines, where the found string is correct, but part of a larger string, that i don't want.
See also the -x option.

I'm sure we could give you much better advice in less time than its taken so far if we had a better description of the bigger picture, what you're trying to accomplish, along with the code to your entire script and the environment in which it runs. For all we know, you may not even be using the best tool for the job.

Regards,
Alister
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