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
# 8  
Old 10-23-2017
Quote:
Originally Posted by knijjar
awk -F'[|=]' 'FNR==NR{a[$0];next} ($13 in a)' Input_file1 Input_file2

Which parameter is in $13 from Input_file2
You can find out, yourself, with a little test:
Code:
echo "SWITCHFEED|~|VENDORNAME=CISCO|~|VENDORMODEL=CISCO2192|~|SWITCH=XYZ1|~|IPADDRESS=111.44.232.243|~|" | /usr/xpg4/bin/awk -F"[=|]" '{print $13}'

Output:
Code:
111.44.232.243

or
Code:
/usr/xpg4/bin/awk -F"[=|]" '{print $13}' file2

# 9  
Old 10-23-2017
Quote:
Originally Posted by knijjar
awk -F'[|=]' 'FNR==NR{a[$0];next} ($13 in a)' Input_file1 Input_file2
Which parameter is in $13 from Input_file2
Hello knijjar,

$13 represents the IP address column of Input_file2, I am adding complete explanation of code here, please do let me know if you have any queries on same.
Code:
awk -F'[|=]'   '     ##making field separator as |(pipe) and = here.
FNR==NR{             ##FNR==NR condition will be TRUE only when first Input_file named Input_file1 is being read, FNR and NR both represents the number of lines in an Input_file the only difference is FNRs value gets RESET once awk starts reading next Input_file and NRs value will be keep increasing till all the Input_file(s) are read.
a[$0];               ##creating an array named a whose index is current line of Input_file1.
next                 ##next is awk in-built keyword which skips all further statements.
}
($13 in a)           ##This condition will be running only when second Input_file named Input_file2 is being read, checking here if $13(field 13) which has IP address of Input_file2 is present in array a or NOT if present then no action is mentioned, so default action print of current line of Input_file2 will happen here.
' FiLE1 FiLE2        ##Mentioning names of Input_file(s), Input_file1 and Input_file2 here.

Thanks,
R. Singh
# 10  
Old 10-23-2017
Thanks RavindeSingh13 / Aia, for the solution you guys provided, it has worked, this is exactly what I was looking for. Made my life so easy !!! FYI, I will remember to post the OS and the version upfront going forward
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