Comparing lists of things…

Obviously when I want to compare two files I’m going to use  diff .

The trouble is that while it does a really good job of comparing two files if all you want to know what the differences between two lists are it can be difficult to see what is going on as the lists get longer.

You can simplify things using  grep  to display only the rows that are different and  sort  to sort the output so all the differences are grouped together.

$ cat test.a
Adam  12345
Wendy 23456
Bob   34567
Sam   45678
Joe   56789
cat test.b
Adam  12345
Wendy 23456
Joe   56789
Chad  1000
diff test.a test.b |grep '^[<>]' | sort -k 1
< Bob   34567
< Sam   45678
> Chad  1000

However, I’m not good at remembering things and I wanted to make it clearer which items in the list had been removed and which ones had been added so I decided to write a short script to take advantage of some of the more verbose output formatting options available when using  diff .

# Compares two files and prints out all the additional lines in each. Useful
# for compairing lists of things.
# This  program  is free software: you can redistribute it and/or modify  it
# under the terms of the GNU General Public License as published by the Free
# Software  Foundation, either version 3 of the License, or (at your option)
# any later version.
# This  program  is  distributed  in the hope that it will  be  useful,  but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  License
# for more details.
# You  should  have received a copy of the GNU General Public License  along
# with this program. If not, see <>
#  12 Sep 18   0.1   - Initial version - MEJT
# Scan command line arguments
while test $# -gt 0; do
  case "$1" in
    echo "syntax: $0 FILE FILE"
    exit 0 ;;
  -*) # Unrecognized qualifier!
    echo "$0: unrecognized option '$1'"
    echo "Try '$0 --help' for more information."
    exit 0 ;;
  *) # Append each argument to args[] (preserving quoted strings).
    _count=$((_count+1)) ;;
# Compare the two files.
if [ "$_count" -eq 2 ];then
  diff --new-line-format="" --old-line-format="- %L" \
  --unchanged-line-format="" ${_args[0]} ${_args[1]} | sort -k 1
  diff --new-line-format="+ %L" --old-line-format="" \
  --unchanged-line-format="" ${_args[0]} ${_args[1]} | sort -k 1
  echo "$0: wrong number of arguments."
  echo "Try '$0 --help' for more information."
  exit -1
# Exit!
exit $?

Sorting the output (in the script) makes it easy to see what items were added or removed..

./ test.a test.b
- Bob   34567
- Sam   45678
+ Chad  1000

This entry was posted in Linux and tagged . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.