Skip to main content
3 of 4
deleted 92 characters in body
Rui F Ribeiro
  • 58k
  • 28
  • 156
  • 239

Compare 2 files and store the output as file1_value,file2_value,Match/NoMatch

I have 2 files,

file1 ->

1 2 2 3 5 

file2 ->

1 3 2 6 

I want to the output to be stored in a 3rd file called file3 as

1,1,Match 2,2,Match 2,,NoMatch 3,3,Match 5,,NoMatch ,6,NoMatch 

I am new to shell scripting and I've tried,

sort file1 > file1sorted.txt sort file2 > file2sorted.txt # Combine the sorted files with a comma and store it in a new file paste -d ',' file1sorted.txt file2sorted.txt > mergedsortedfile.txt # Compare the columns and store the result in a new file awk -F',' '{print $1 == $2 ? "MATCH" : "NO MATCH"}' mergedsortedfile.txt > result.txt # Merge the result file with the already existing merged file paste -d ', ' mergedsortedfile.txt result.txt > final_result.txt 

The result appears like this,

1,1,MATCH 2,2,MATCH 2,3,NO MATCH 3,6,NO MATCH 5,,NO MATCH