Ben
Ben

Reputation: 6907

unix - diff command to output in single-line-per-difference format

The version of diff in my cygwin has a number of advanced options which allow me to print out one difference per line.

Given two files one.txt and two.txt.

one.txt:

one
two
three
four
five
six

two.txt

one
two2
three
four
five5
six

And running diff in cygwin with the following options/parameters:

diff -y --suppress-common-lines one.txt two.txt

Gives an output of:

two   |two2
five  |five5

This is the type of format I'm after whereby one difference is printed out per line. On my dev solaris box, the "-y" option is not supported, so I'm stuck with an output which looks like this:

2c2
< two
---
> two2
5c5
< five
---
> five5

Does anyone know of a way I can get an output of one difference per line on this solaris box? Maybe using a sed/awk one liner to massage the output from this more primitive diff output? (Please note, I am not able to install a more up-to-date diff version on this solaris box).

Thanks!

Upvotes: 0

Views: 15453

Answers (5)

sunil k
sunil k

Reputation: 29

All the answers given above and below are perfect but just typing a command and getting a result wont help you in solving similar problems in future.

Here a link which explains how diff works. once you go through the link, you can the problem yourself

Here is a link. https://www.youtube.com/watch?v=5_dyVrvbWjc

Upvotes: 1

Thomas
Thomas

Reputation: 41

Example output:

# ~/config_diff.sh postfix_DIST/master.cf postfix/master.cf
postfix_DIST/master.cf: -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject -o smtpd_tls_wrappermode=yes -o smtp_fallback_relay=
postfix/master.cf: -o cleanup_service_name=cleanup_sasl -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject -o cleanup_service_name=cleanup_sasl -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject -o smtp_fallback_relay=

postfix_DIST/master.cf:smtp inet n - - - - smtpd smtp unix - - - - - smtp
postfix/master.cf:smtp inet n - - - - smtpd smtp unix - - - - - smtp

Sadly, it cannot currently handle several same configurations variables ... it counts them and will think the the files differ.

Upvotes: 1

Thomas
Thomas

Reputation: 41

#! /bin/bash

FILES="$@"

COLUMN=1

for variable in $( awk -F: '{print $X}' X=${COLUMN} ${FILES} | sort -u ) ; do
        NUM_CONFIGS=$( for file in ${FILES} ; do
                grep "^${variable}" ${file}
        done | sort -u | wc -l )
        if [ "${NUM_CONFIGS}" -ne 1 ] ; then
                for file in ${FILES} ; do
                        echo ${file}:$( grep "^${variable}" ${file} )
                done
                echo
        fi
done

Upvotes: 0

wnoise
wnoise

Reputation: 9942

comm -3 almost does what you want, but requires sorted input. It also will put them in separate lines by alphabetical order. Your example (once sorted) would show up as

five
      five5
two
      two2

If solaris diff won't do what you want, then nothing on a standard solaris box is liable to do so either, which means introducing code from elsewhere, either your own or someone else's. As GNU diff does what you want, just use that.

Upvotes: 1

Andy Lester
Andy Lester

Reputation: 93805

Use GNU diff.

http://www.gnu.org/software/diffutils/

You can build and install it into your local directory, no? If you have a home directory and a compiler and a make, you can build your own GNU diff.

I don't have Solaris, but I can't imagine it would be much more than this:

./configure --prefix=/home/bob
make
make install

No root privileges required.

Upvotes: 2

Related Questions