You are not logged in.

#1 2015-11-04 19:30:54

rklingsten
Member
Registered: 2009-07-31
Posts: 29

[SOLVED] Compare two files for common strings NOT line-by-line

Howdy, I need to compare two files and output the line in file 2 which matches a string from file 1. As you will see I am trying to get a MAC to IP mapping from the MAC and ARP tables on Cisco switches/routers. Thanks for any suggestions!

file 1:

30,0040.9d58.5879,dynamic,ip,ipx,assigned,other,GigabitEthernet7/22,
130,0007.4d22.992b,dynamic,ip,ipx,assigned,other,GigabitEthernet7/34,
130,0007.4d25.5b80,dynamic,ip,ipx,assigned,other,GigabitEthernet7/33,
130,0007.4d30.31aa,dynamic,ip,ipx,assigned,other,GigabitEthernet7/31,

file 2:

Internet  10.21.198.231           0   6c3b.e53a.a109  ARPA   Vlan130
Internet  10.20.83.115          128   0007.4d22.992b  ARPA   Vlan130
Internet  10.20.85.117            0   bc30.5b9e.733e  ARPA   Vlan130
Internet  10.20.80.113            0   c434.6b71.8e79  ARPA   Vlan130

output:

Internet  10.20.83.115          128   0007.4d22.992b  ARPA   Vlan130

Last edited by rklingsten (2015-11-04 20:04:39)

Offline

#2 2015-11-04 19:41:28

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] Compare two files for common strings NOT line-by-line

Using Awk (tricky and fragile because of the different field separators):

awk -F'[, ]' 'FNR==NR{a[$2];next};($16 in a)' file1 file2
Internet  10.20.83.115          128   0007.4d22.992b  ARPA   Vlan130

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2015-11-04 19:49:50

rklingsten
Member
Registered: 2009-07-31
Posts: 29

Re: [SOLVED] Compare two files for common strings NOT line-by-line

Thanks so much! It's close, but not working, dumping all of file 2 ...

I did try your code on the example lines I posted and it works there. Can you explain a little bit how the code works, maybe I can figure out where it's busting... I have never used awk.

Offline

#4 2015-11-04 20:03:50

rklingsten
Member
Registered: 2009-07-31
Posts: 29

Re: [SOLVED] Compare two files for common strings NOT line-by-line

Aha got it! Your script was excellent in pointing the way. I processed the one file more to get rid of competing seperators in favor of comma. Then I did this:

awk -F',' 'NR==FNR{a[$2];next}$4 in a{print $2}' file1 file2

Thanks again!

Offline

#5 2015-11-04 21:22:22

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,772

Re: [SOLVED] Compare two files for common strings NOT line-by-line

Grrr....  I just realized you edited the first post -- That is why I could not figure our why Jason was using $16.   The input files changed format!

Let's see if my awk is improving....

So if NR equals FNR you are reading the first file and have not yet found the second file.  If you are in the first file, add the second parameter (the MAC) to an an associative array as an index with no assigned value, then skip to process the next record.  If you are not in the first file the record is from the second file, so pick the MAC based upon its known position in the records of the second file (field 4).  See if that MAC is in the associative array, and if it is spew forth the IP address from the current record field 2.

Jason, is reading a file in in that manner a common construct in awk, or are you just a frigin awk wizard?

Last edited by ewaller (2015-11-04 23:09:53)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#6 2015-11-04 23:00:45

manchoz
Member
From: Turin, Italy
Registered: 2015-09-27
Posts: 2
Website

Re: [SOLVED] Compare two files for common strings NOT line-by-line

Hi,
this is an alternative one-liner solution using just coreutils and some unusual Bash feature.

join -1 2 -2 4 <(<file1 tr ',' ' ' | sort -k2) <(<file2 sort -k4) | cut -d' ' -f 10-

Let me know if it works for you :-)


– Code is in the eye of the beholder –

Offline

#7 2015-11-04 23:01:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] Compare two files for common strings NOT line-by-line

Yes it is one of the standard Awk idioms (sadly, I cannot claim any wizardry).

Nicely interpreted, ewaller: that saved me quite some typing tongue


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2015-11-05 05:15:55

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 652

Re: [SOLVED] Compare two files for common strings NOT line-by-line

I just do these kind of things the simple way:

#!/bin/bash
while read line; do
    set -- $line
    if grep -q $4 file1; then
        echo $line
    fi
done <file2

Offline

Board footer

Powered by FluxBB