Compare two Files and Print Lines that Match

December 15, 2018

The proper way to compare two files and print lines that match:

awk 'NR==FNR{arr[$0];next} $0 in arr' file1.txt file2.txt

Here is a shell script you can run:

# Filename:
# source:
# this file takes two text files as input
# sorts them and outputs lines from
# file 2 that match file 1

if [ -f "$1" ] && [ -f "$2" ]
    awk 'NR==FNR{arr[$0];next} $0 in arr' $1.tmp $2.tmp;
    echo "Usage: file1 file2";
    echo "where file1 is the 'master' file";
    echo "and file2 is the file possibly containing duplicates";