Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pick the exact IP Address 2

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

I need to pick "172.21.3.1" from the list of IP Addresses below (grep isn't the command for this because it will pick all these addresses).

172.21.3.1
172.21.3.11
172.21.3.12
172.21.3.13
172.21.3.100

Is there any command better than grep? I have an idea to use "wc -c" command but I find it difficult to construct the loop.

Please help.
 
grep -w may do it, if its available on your platform. Otherwise grep '^172.21.3.1$' would do the trick.

Annihilannic.
 
Actually, I should point out that the second solution will only match the IP address if it occupies the whole line. If it there may be other text on the line, try grep '\<10.1.3.1\>'

Annihilannic.
 
grep -F -x "172.21.3.1" /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi Annihilannic,

I'm very thankful I discovered this forum !!! You're superb :) you were able to give the solution straight away ...

I think I will be using "grep -w" because I will incorporate this in a loop, meaning my IP Address will be in a variable (it is easy to do "grep -w $IP"

I will try to use the second solution also ... how can I use a variable on this one? Is it like this:

grep "\<$IP\>"


Many thanks again ...

Maria :)
 
Yes, grep "\<$IP\>" should work. PHV's solution will also only match entire lines by the way. -F is a good idea too because "." can match any character, so maybe you should use grep -Fw.

Annihilannic.
 
Hi PHV,

The "grep -F -x "172.21.3.1" /path/to/input" solution didn't work, it says:

grep: illegal option --F
grep: illegal option --x
Usage: grep -hblcnsviw pattern file . . .

Thanks anyway :)
Maria
 

-F isn't working in my case even if I incorporated it with grep -Fw ...

In my input file, only one IP Address is listed per line anyway ... as long as "grep -w" will pick the exact IP Address that would be brilliant :)

Thanks!
 

"fgrep" isn't good ... the output is just like using the normal "grep"

Thanks again :)
 
Another way:[tt]
IP="172.21.3.1"
awk '$1=="'$IP'"' /path/to/input[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

It did work !!! This is useful if there are other characters in the same line ... and I can specifically ask the script to look in the first column only.

Cheers,
Maria :)
 
Since you want to match entire lines I would recommend using fgrep -x. To show you the difference:

[tt]$ cat /tmp/t
172.21.3.111
172.21.311.1
172.21.3.1
$ grep -w 172.21.3.1 /tmp/t
172.21.311.1
172.21.3.1
$ fgrep -x 172.21.3.1 /tmp/t
172.21.3.1
[/code]

(I know 311 is not valid for an IP, but just for example...)

Annihilannic.
 

Hi Annihilannic,

Thanks for pointing out this one, this will guarantee that I will definitely pick the right IP Address.

I did some experiment with "fgrep -x", if I add any character on the line where 172.21.3.1 is located it will definitely produce nothing, meaning it will definitely look for the exact match.

Cheers!
Maria :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top