CSP CSP - 1 year ago 39
Bash Question

Compare two columns of two files and display the third column with input if it matching of not in unix

I would like to compare the first two columns of two files file1.txt and file2.txt and if they match to write to another file output.txt with the third column of both file1,file 2 along with details if it matches or not .

file1.txt

ab|2001|name1
cd|2000|name2
ef|2002|name3
gh|2003|name4


file2.txt

xy|2001|name5
cd|2000|name6
ef|2002|name7
gh|2003|name8


output.txt

name1 name5 does not match
name2 name6 matches
name3 name7 matches
name4 name8 matches

Answer Source

You can use paste and awk to get what you want.

Below solution is assuming the fields in file1 and file2 will be always delimited by "|"

paste -d "|" file1.txt file2.txt | awk -F "|" '{ if( $1 == $4 && $2 == $5 ){print $3, $6, "matches"} else {print $3, $6, "does not match"} }' > output.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download