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 .

#!/bin/bash
#
# sh-compare-files.sh
#
# 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 <http://www.gnu.org/licenses/>
#
#  12 Sep 18   0.1   - Initial version - MEJT
#
_count=0
 
# Scan command line arguments
while test $# -gt 0; do
  case "$1" in
  --help)
    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).
    _args[$_count]=$1
    _count=$((_count+1)) ;;
  esac
  shift
done
 
# 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
else
  echo "$0: wrong number of arguments."
  echo "Try '$0 --help' for more information."
  exit -1
fi
 
# Exit!
exit $?

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

./sh-compare-files.sh test.a test.b
- Bob   34567
- Sam   45678
+ Chad  1000
$

Advertisements
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:

WordPress.com Logo

You are commenting using your WordPress.com 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.