Mar 19

Comparing two lists in Linux/UNIX

Deserves its own blog post this one.  A new favourite of mine, that seems more reliable than using diff or comm on account of its being easy to understand, and thus less likely to get wrong (matter of opinion).

grep -Fxv -f masterlist backupclients which would list any lines in a list of backupclients that were not found in the master list.

Note:  This lists any lines in the second file that are not matched in the first file (not the other way around).

-F pattern to match is a list of fixed strings

-x select only matches that match the whole line

-v reverses it, i.e. does not match the whole line

-f list of strings to (not) match are in this file

Result: filename output entire lines from the file specified that are not in the file specified by –f

 

It can be useful to convert all text to UPPERCASE and don’t forget to REMOVE EMPTY LINES in files.

Text can be converted to uppercase with tr ‘[:lower:] [:upper:]’ <infile >outfile

If you want to list only the lines that appear in two files, then comm -12 firstfile secondfile works well.

Facebooktwittergoogle_plusredditpinterestlinkedinmail
1 comment

1 comment!

  1. This is a very good tip especially to those new to the blogosphere.
    Brief but very precise information… Thanks for sharing this one.

    A must read article!

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *