How to compare the columns in two .csv files?

Hi

I have to compare two .csv files which having 4 columns and i am expecting the output if there is difference in the 3,4columns in two files with respect to the first column. if my statement is not clear please refer the example.
Input:
-----
File 1 :

hostname MAC SWITCH_IP SWITCH_PORT
01HW1234 000 172.20.1.0 Fastethernet0
01HW4567 111 172.20.1.1 Fastethernet1

FILE 2:

hostname MAC SWITCH_IP SWITCH_PORT
01HW1234 000 172.20.1.1 Fastethernet1
01HW8901 333 172.20.1.2 Fastethernet3
 

output:
-------

hostname MAC SWITCH_IP SWITCH_PORT
01HW1234 000 172.20.1.1 Fastethernet1
 

Note: the comparision is performing with respect to 1st column(hostname).If there is any changes in SWITCH_IP and SWITCH_PORT then it wil display all the values in the file 2.

Thanks in Advance

 
 nawk 'NR==FNR{a[$1]=$0;next}($1 in a) {print}' file1 file2

Thanks to shahul for this code.

But in the expected output, you have skipped the differed rows!

awk 'NR==1{print}NR==FNR{a[$1]=$0;next}($1 in a)&&$0!=a[$1]' file1 file2

The code :

nawk 'NR==FNR{a[$1]=$0;next}($1 in a) {print}' file1 file2

May not behave the way you expect since it print lines whose $1 appear in both file in all case, even in the case the line is unchanged in both file

1 Like

Thanks for your update. But it this "nawk" is not working in my server. Please find the version of my linux server.

uname -a
Linux unixteam.tcs.com 2.6.9-5.ELsmp #1 SMP Wed Jan 5 19:30:39 EST 2005 i686 i686 i386 GNU/Linux

---------- Post updated at 08:32 AM ---------- Previous update was at 08:28 AM ----------

Thanks a lot CTSGNB!! It's working fine!!! Have a Great day!!

nawk 'NR==FNR{a[$1]=$1;b[$0]=$0;next}($1 in a)&& !($0 in b)' file1 file2

Thanks
SHa

1 Like
nawk 'FNR==NR {f1_3[$1]=$3;f1_4[$1]=$4;next} FNR==1{print;next}$1 in f1_3 && ($3!=f1_[$3] || $4!=f1_4[$1])' file1 file2

Example :

$ cat f1
hostname MAC SWITCH_IP SWITCH_PORT
01HW1234 000 172.20.1.0 Fastethernet0
01HW4567 111 172.20.1.1 Fastethernet1
01HW1234 000 172.20.1.0 Fastethernet2
$ cat f2
hostname MAC SWITCH_IP SWITCH_PORT
01HW1234 000 172.20.1.1 Fastethernet1
01HW8901 111 172.20.1.1 Fastethernet1
01HW1234 000 172.20.1.0 Fastethernet2
$ nawk 'NR==FNR{a[$1]=$0;next}($1 in a) {print}' f1 f2
hostname MAC SWITCH_IP SWITCH_PORT
01HW1234 000 172.20.1.1 Fastethernet1
01HW1234 000 172.20.1.0 Fastethernet2

---> also display unchanged lines

$ nawk 'NR==1{print}NR==FNR{a[$1]=$0;next}($1 in a)&&$0!=a[$1]' f1 f2
hostname MAC SWITCH_IP SWITCH_PORT
01HW1234 000 172.20.1.1 Fastethernet1

--->does not display unchanged lines (except the header line of course)

---------- Post updated at 03:53 PM ---------- Previous update was at 03:37 PM ----------

or more simply

nawk 'NR==FNR{a[$1]=$3$4;next}($1 in a)&&($3$4!=a[$1])||FNR==1' file1 file2
2 Likes