Compare two Files and Print Lines that Match

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:

#!/bin/bash
# Filename: showdupes.sh
# source: http://brakertech.com/compare-two-files-and-print-lines-that-match/
# 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" ]
then
    awk 'NR==FNR{arr[$0];next} $0 in arr' $1.tmp $2.tmp;
else
    echo "Usage: dedupe.sh file1 file2";
    echo "where file1 is the 'master' file";
    echo "and file2 is the file possibly containing duplicates";
fi

Leave a Reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>