I have a file File1 with data as


And another file File2 with data as:


I need to compare the File1 data with column2 data of File2 and print the matching into a one file and the non-matching into another.

  • 1
    Do you need just the data from File1, or also the extra columns that are only in File2? Is the order of lines important? – Gilles 'SO- stop being evil' Mar 10 '16 at 22:19
  • What would the correct output be for this example? – Alexander Mar 12 '16 at 8:14
awk -F '
  !b{a[$0]; next}
  $2 in a {print > "matching.txt"; next}
  {print > "non-matching.txt"}' file1 b=1 file2

Or with two passes and assuming the files are sorted on the join key:

join -t , -2 2 -o 2.1,2.2,2.3 file1 file2 > matching.txt
join -t , -2 2 -v 2 -o 2.1,2.2,2.3 file1 file2 > non_matching.txt

This one-liner uses cut to pick out the right field and grep to find matches. Matches and non-matches are appended to the files named Matching and NonMatching, respectively.

for x in $(cut -d, -f2 File2); do grep -q "$x" File1 && echo "$x" >> Matching || echo "$x" >> NonMatching; done


Here is the command for matching records -

awk -F, 'FNR==NR{f1[$1]=$0;next}$2  in f1{print $0}' OFS="," file1 file2 > matchedRecords.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.