Grep exact IP address from file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep exact IP address from file
# 1  
Old 02-04-2013
Grep exact IP address from file

I have a file with a lot of IP addresses in it named "address.list".

address.list looks something like this:
10.77.50.11
10.77.50.110
10.77.50.111
a bunch more addresses

For every IP address I need to grep another file to see if the IP address is in the other file:

Code:
for x in `cat address.list` ;
do echo "results for $x" ;
grep $x someconfig.file ;
echo "---------------------------------------------" ;
done

I get results like this:

results for 10.77.50.11
node 10.77.50.11 {
node 10.77.50.110 {
node 10.77.50.111 {
node 10.77.50.112 {
node 10.77.50.113 {
node 10.77.50.114 {
node 10.77.50.115 {
node 10.77.50.116 {
node 10.77.50.117 {
---------------------------------------------

I need results for only 10.77.50.11 and not 10.77.50.110 etc

How would I tweak my script to just give me the exact address I'm looking for?

Thanks!
# 2  
Old 02-04-2013
That is Dangerous Backticks: for x in `cat address.list`

Use a while loop instead and grep with -w option:
Code:
while read x
do
  echo "results for $x"
  grep -w "$x" someconfig.file
  echo "---------------------------------------------"
done < address.list

From grep manual:
Code:
-w Select only those lines containing matches that form whole words.

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-04-2013
I'd do that by telling grep the previous and next characters must either be beginning of line, end of line, or non-numeric.

Once you've done that you can grep entire lists of things with -f.

Code:
while read IP
do
        IP="${IP//./\\.}" # Turn . into \., since . is special to awk
        echo "(^|[^0-9])${IP}($|[^0-9])"
done < address.list > address.regex

egrep -f address.regex configfile

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 02-04-2013
Quote:
Originally Posted by squoggle
[...]
For every IP address I need to grep another file to see if the IP address is in the other file
Could you post a sample from the second file?
This User Gave Thanks to radoulov For This Post:
# 5  
Old 02-04-2013
hm, my man page of grep says:

Code:
 -f FILE, --file=FILE
	      Obtain  patterns	from  FILE, one per line.  The empty file con-
	      tains zero patterns, and therefore matches nothing.

So i suppose

Code:
grep -wf address.list /path/to/other.file

would do the same as the loop, no?

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 02-04-2013
Great responses. I was able to get it sorted out "grep -w" was the ticket!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep -w ip address from a file isnt working need help

I have a file "file1" that contains several ip address , and the "file2" contains several records , each line in file2 contains somewhere the ip address that i am searching in the file1 I use the unix command grep -w for i in `cat file1` do grep -w "$i" file2 >> file3 done ... (9 Replies)
Discussion started by: knijjar
9 Replies

2. Shell Programming and Scripting

Grep for an exact match in a file

I am currently having some issues while trying to grep for a exact string inside a file. I have tried doing this from command line and things work fine i.e. when no match is found, return code=1 but when its done as part of my script it returns 0 for the same command - I dont know if there is an... (6 Replies)
Discussion started by: Ads89
6 Replies

3. UNIX for Dummies Questions & Answers

Grep for ip address only from a file

Facing issues in grepping only the IP Address from a file i have tried the below and it was of not much help awk -F"" '/(/ { print $3 }' awk -F"</*(>" '/ip/ { print $2 }' grep "ip" file1|cut -f2 -d"<"|cut -f2 -d">" grep "(" file1 |cut -f2 -d"<"|cut -f2 -d">" grep -e... (9 Replies)
Discussion started by: satishcarya
9 Replies

4. Shell Programming and Scripting

Grep IP address form a text file along with the subnet

I have an input file: class 1 3 5 10.10.10..0/23 hicks jimmy class 3 10.12.10.0/22 mike class.019283 10.10.15.10/20 henry gym.847585 45 192.168.10.0/22 nancy jim steve maya The output should look like this: 10.10.10..0/23 10.12.10.0/22 10.10.15.10/20 192.168.10.0/22 I have the... (3 Replies)
Discussion started by: e_mikey_2000
3 Replies

5. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

6. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

7. Shell Programming and Scripting

grep ip address from file

I have a file $ cat ip12 11.22.33.44 192.68.1.2 helo l 72.34.34.200 333.444.555.666 12.23e.544.423 myip1 11.22.33.44 myip2 33.44.55.66 #fine this IP should also be listed I do $ cat ip12 | grep '^\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}$' 11.22.33.44 192.68.1.2 (2 Replies)
Discussion started by: anil510
2 Replies

8. Shell Programming and Scripting

grep exact string from a file

Hi, I have the following output from a file zone "adm.test.com" { abc test1.db } zone "test.com" { xyz test2.db } zone "1.test.com.sg" { 1abc test3.db } zone "3.test.com.uk" { 1xyz test4.db } (6 Replies)
Discussion started by: vchee
6 Replies

9. Shell Programming and Scripting

grep search for http address in a file.

Hi I have downloaded a HTM file from the web. What I want to do is perform a grep search of that file, searching for all strings where 'http' is present within the file, but only contains the word 'cache' within the string. I've includeda sample file, which I'm trying to extract the above... (5 Replies)
Discussion started by: colmbell
5 Replies

10. Shell Programming and Scripting

Want to grep exact pattern from file

Contents of my file is: DI DI DIR PHI I want to extract only DI. I am using below command grep -w DI <file> but it is also extracting DI. Can i use any other command to extract exact pattern where '[' does not have special meaning (4 Replies)
Discussion started by: nehashine
4 Replies
Login or Register to Ask a Question