Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

shell - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Welcome to stack overflow, could you please try following and let me know if this helps you.

awk -F"|" 'FNR==NR{a[$2]=$1;b[$2]=$3;next} ($2 in a) && ($1==a[$2]){print b[$2],$3,"matched properly.";next} {print b[$2],$3,"Does NOT matched."}' file1.txt file2.txt

EDIT: Adding a non-one liner form of solution too here.

awk -F"|" '
FNR==NR{
   a[$2]=$1;
   b[$2]=$3;
   next
}
($2 in a) && ($1==a[$2]){
   print b[$2],$3,"matched properly.";
   next
}
{
   print b[$2],$3,"Does NOT matched."
}
' file1.txt file2.txt

Explanation: Adding explanation for above code.

awk -F"|" '                                 ##Starting awk program from here and setting field separator as | here.
FNR==NR{                                    ##Checking condition if FNR==NR which will be TRUE when file1.txt is being read.
   a[$2]=$1;                                ##Creating an array with named a whose index is $2 and value is $1.
   b[$2]=$3;                                ##Creating an array named b whose index is $2 and value is $3.
   next                                     ##next will skip all further statements from here.
}                                           ##Closing BLOCK for FNR==NR condition here.
($2 in a) && ($1==a[$2]){                   ##Checking condition if $2 is in array a AND first field is equal to value of array a value of index $2.
   print b[$2],$3,"matched properly.";      ##Printing array b value with index $2 and 3rd field with string value matched properly.
   next                                     ##next will skip all statements from here.
}                                           ##Closing BLOCK for above condition here.
{
   print b[$2],$3,"Does NOT matched."       ##Printing value of array b with index $2 and 3rd field with string Does NOT matched here.
}
' file1.txt file2.txt                       ##Mentioning Input_file names here.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...