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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep -w ip address from a file isnt working need help
# 1  
Old 10-22-2017
Grep -w ip address from a file isnt working need help

I have a file "file1" that contains several ip address [only ip address that i want to search in file2], and the "file2" contains several records [close to 100000], each line in file2 contains somewhere the ip address that i am searching in the file1

I use the unix command grep -w
Code:
for i in `cat file1`
do
grep -w "$i" file2 >> file3
done

Code:
Sample Content of "file1" is shown below:

111.44.232.243
111.33.2.3

Code:
Sample Content of file2
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.44.232.243|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.44.124.111|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.213.222|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.213.129|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.213.204|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.213.180|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.213.8|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.213.232|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.213.233|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.213.244|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.233.17|~|
SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.33.2.3|~|

the desired output in file3 should be the first record from file2 and last record from file2 but because the second ip in file1 is "111.33.2.3" is greps everything from file2 as it treats dots in [.2.3]

Any help in this regard is much appreciated.

Last edited by jim mcnamara; 10-22-2017 at 07:53 PM.. Reason: wrong code tag cleanup
# 2  
Old 10-22-2017
grep -F finds literal strings - it turns off regular expressions (the problem you have with the dot character).
# 3  
Old 10-22-2017
Code:
user1@server2% grep -h
Usage: grep -hblcnsviw pattern file . . .

I dont see the Option grep -F for the grep, I am on SunOS 5.10

---------- Post updated at 06:13 PM ---------- Previous update was at 06:08 PM ----------

Code:
/usr/xpg4/bin/grep
/usr/bin/grep
user1@server2% /usr/xpg4/bin/grep -F
Usage:  grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...]
        grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]... [-f pattern_file]... [file...]
        grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
        grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...]
        grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
        grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... [-f pattern_file]... [file...]

user1@server2% /usr/bin/grep -h
Usage: grep -hblcnsviw pattern file . . .


Last edited by MadeInGermany; 10-23-2017 at 02:33 AM.. Reason: changed icode tags to code tags
# 4  
Old 10-22-2017
Would any of these suggestions work?

Code:
/usr/xpg4/bin/grep -Ff file1 file2 | /usr/xpg4/bin/awk -F"[=|]" '{print $13}' >> file3

or
Code:
/usr/xpg4/bin/awk 'FNR==NR {s[$1]; next} $13 in s {print $13}' file1 FS="[=|]" file2 >> file3

If you want the whole line where the ip is found in file2, then
Code:
/usr/xpg4/bin/grep -Ff file1 file2 > file4

or
Code:
/usr/xpg4/bin/awk 'FNR==NR {s[$1]; next} $13 in s' file1 FS="[=|]" file2 > file4


Last edited by Aia; 10-22-2017 at 09:39 PM..
# 5  
Old 10-22-2017
I do need the whole line where the ip is found in file2,

This solution did work for the examples I provided in this case but

/usr/xpg4/bin/grep -Ff file1 file2 > file4


when I apply the same solution when the file1 has more than 5000 ips that i am trying to search in file2 [which happens to have more than 100000 records ]and it fails,
# 6  
Old 10-22-2017
Hello knijjar,

Please always do mention your O.S details from very first post itself, also try to provide sample Input_file and expected results with all the conditions you have. Could you please try following and let me know if this helps you.
Code:
awk -F'[|=]' 'FNR==NR{a[$0];next} ($13 in a)' Input_file1   Input_file2

On a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk,/usr/xpg6/bin/awk , or nawk.

Thanks,
R. Singh
# 7  
Old 10-23-2017
awk -F'[|=]' 'FNR==NR{a[$0];next} ($13 in a)' Input_file1 Input_file2

Which parameter is in $13 from Input_file2
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Grep with Regular expression now working on file directories

Hello Everyone, I have a file sam1 with the below content SYSYSID;MANDT;/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1 echo $Regex \bSYSYSID\b|\bMANDT\b|\b/SIE/AD_Z0M_INDX\b|\b/SIE/AD_Z0M_KEY1\b cat sam1 | grep -Eo $Regex I expect the result as SYSYSID MANDT /SIE/AD_Z0M_INDX /SIE/AD_Z0M_KEY1... (4 Replies)
Discussion started by: sam99
4 Replies

3. 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

4. UNIX for Dummies Questions & Answers

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: for x in `cat... (5 Replies)
Discussion started by: squoggle
5 Replies

5. 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

6. Shell Programming and Scripting

Code to remove files when corresponding file doesnt exist isnt working.

I am trying to add some code to the begging of a script so that it will remove all the .transcript files, when their is no coressponding .wav file. But it doesnt work. This is the code I have added: for transcriptfile in `$voicemaildir/*.transcript`; do wavfile=`echo $transcriptfile | cut -d'.'... (2 Replies)
Discussion started by: ghurty
2 Replies

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

8. Shell Programming and Scripting

grep pattern in a file not working. please help...

Guys, I have my.mrk file as follows: rs112 rs105 rs154 rs136 ... and my.map file: 7 rs112 0.59 7 rs188 0.63 7 rs105 0.77 7 rs113 0.84 7 rs154 0.92 7 rs111 1.46 7 rs095 1.71 (3 Replies)
Discussion started by: Zoho
3 Replies

9. Shell Programming and Scripting

why isnt cron working?

I am using Ubuntu linux desktop, and I am trying to schedule a sheel script to run every 10minutes. These are the steps I have taken: crontab -e added and saved this line to the file # m h dom mon dow command */1 * * * * /home/enzo/Desktop/dlrecentuse restarted cron: sudo... (5 Replies)
Discussion started by: daydreamer
5 Replies

10. Shell Programming and Scripting

Why passwd isnt working in shell scripts?

I had to write a script to change my login password, and the script wasnt working fine. When I searched through the previous postings in this forum, I got the solution (using 'expect' tool). But I would like to know why passwd command isnt working in scripts? (1 Reply)
Discussion started by: Deepa
1 Replies
Login or Register to Ask a Question