Help with showing the difference in two lines of input

I would like my script to be able to tell the difference between to lines of input, like:

Input 1:
1 2 3 4 5
Input 2:
1 2 3 4 5 6

I want the script to tell me that the difference between the two lines is the 6. Is there anyway I can have it do this?

Here's an example of what my script looks like now:

#!/bin/bash
echo "Input numbers"
read input1
echo "Input numbers"
read input2

if [ "$input1" == "$input2" ]
then
echo "Match"
else
echo "No match"
fi

Only I need it to be more specific, to tell me where the two lines of input differ.

Thanks in advance!

Here is one solution using awk, considering that numbers are separated by single spaces in between, otherwise change the delimiter at the split function :

#!/bin/bash

echo "Input numbers"
read input1
echo "Input numbers"
read input2

if [ "$input1" == "$input2" ]
then
echo "Match"
else
echo    "  The numbers that don't match are: "

 awk -v a="$input1" -v b="$input2" 'BEGIN {x=split(a,arra," ");y=split(b,arrb," ")
  
     if      ( x > y)
                { for ( i=1; i<=x; i++)
                        if ( arra != arrb )
                            print arra " <---> " arrb
                                                                   exit } 
     else if ( x < y )
              { for ( i=1; i<=y; i++)
                         if ( arra != arrb )
                            print arra " <---> " arrb
                                                                    exit } 
      else  ( x = y )
              { for (i=1; i<=x; i++)
                        if ( arra != arrb)
                           print arra " <---> " arrb
                                                                    exit }    
}'

fi

rubin,

Thanks a lot for looking into it! It works perfectly for the example I gave, however if I enter something like this:

input1= 1 2 3 4 5 80 90

input2= 1 2 3 4 5 6 80 90

I get this as a result:

6 <--> 80
80 <--> 90
90 <-->

even though the only difference between the two inputs is the 6. Is there a solution to this bug?

#!/bin/bash

echo "Input numbers"
read input1
echo "Input numbers"
read input2

if [ "$input1" == "$input2" ]
then
echo "Match"
else
echo " The numbers that don't match are: "

awk -v a="$input1" -v b="$input2" 'BEGIN {x=split(a,arra," ");y=split(b,arrb,"
")

 if      \( x &gt; y\)
            \{ for \( i=1; i&lt;=x; i\+\+\)
                    if \( arra [i]!= arrb [i]\)
                      \{
                       if \( arra [i]== arrb[i\+1] \)
                          \{
                           print arrb
                           [i]break
                           \}
                       else if \( arra[i\+1] == arrb [i]\)
                           \{
                           print arra
                           [i]break
                           \}
                       else
                       print arra [i]" &lt;---&gt; " arrb
                      [i]\}
                                                               exit \}
 else if \( x &lt; y \)
          \{ for \( i=1; i&lt;=y; i\+\+\)
                     if \( arra [i]!= arrb [i]\)
                      \{
                       if \( arra [i]== arrb[i\+1] \)
                          \{
                           print arrb
                           [i]break
                           \}
                       else if \( arra[i\+1] == arrb [i]\)
                           \{
                           print arra
                           [i]break
                           \}
                       else
                       print arra [i]" &lt;---&gt; " arrb
                      [i]\}
                                                                exit \}
  else  \( x = y \)
          \{ for \(i=1; i&lt;=x; i\+\+\)
                    if \( arra [i]!= arrb[i]\)
                      \{
                       if \( arra [i]== arrb[i\+1] \)
                          \{
                           print arrb
                           [i]break
                           \}
                       else if \( arra[i\+1] == arrb [i]\)
                           \{
                           print arra
                           [i]break
                           \}
                       else
                       print arra [i]" &lt;---&gt; " arrb
                      [i]\}
                                                                exit \}

}'

fi

Hi.

This command may be available to you:

#!/usr/bin/env sh

# @(#) s1       Demonstrate "word" differences.

#  ____
# /
# |   Infrastructure BEGIN

echo
set -o nounset

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) wdiff
set -o nounset

echo

a=${1-data1}
echo " Input file $a:"
cat $a
b=${2-data2}
echo " Input file $b:"
cat $b

# |   Infrastructure END
# \
#  ---

echo
echo " Results from processing:"

wdiff -3 $a $b

exit 0
Producing:
% ./s1 data3 data4

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU wdiff 0.5

 Input file data3:
1 2 3 4 5 80 90
 Input file data4:
1 2 3 4 5 6 80 90

 Results from processing:

======================================================================
 {+6+}
======================================================================

See man wdiff for details ... cheers, drl

Thanks for the help, but this doesn't work quite right either.

If for example, I enter the following:

Input1: 1 2 6 4 70
Input2: 1 6 2 4 80 90 670

I get the following response:

Nodes that do not match are:

6

Even though there are many more mismatches than just the 6.

Anyway to fix this? Thanks again!

Thanks again for your help drl, but I apparently don't have the wdiff command available to me. Any other ideas? Thanks!

Supposing one line per file:

awk '
NR==FNR{sub(/\n/,"");a1[$0]=$0;next}
{sub(/\n/,"");a2[$0]=$0;if ( ! ( $0 in a1 ) ){print "In file 2 but not in 1:"$0}}
END {for ( i in a1 ) 
    if  ( ! ( i in a2 ) ) {print "In file 1 but not in 2:"i}
}' RS=' '  f1 f2

In Python try this:

from sets import Set
a = Set([1,2,3,4,5,6,80,90])
b = Set([1,2,3,4,5,80,90])
for i, v in enumerate(a^b):
	print v

>>> 6

You may not have wdiff, but you still have diff, yes? "man diff" should give you any information you'll need.

bakunin

Thanks for looking into it Klashxx,

When I try your suggestion, and say I input these numbers:

255 256 280 290

I get this error:

awk: cmd. line:6: fatal: cannot open file `255' for reading (No such file or directory)

It always says it can't open the first number. I'm not good with awk, do you see where the problem is?

Thanks,

Kweekwom

Hi , u need to put the input lines in files, for ex:

$ cat file1
255 256 280 290 15
$ cat file2
255 256 280 290 4 5

And then:

$ awk '
NR==FNR{sub(/\n/,"");a1[$0]=$0;next}
{sub(/\n/,"");a2[$0]=$0;if ( ! ( $0 in a1 ) ){print "In line 2 but not in 1:"$0}}
END {for ( i in a1 ) 
    if  ( ! ( i in a2 ) ) {print "In line 1 but not in 2:"i}
}' RS=' '  file1 file2
In line 2 but not in 1:4
In line 2 but not in 1:5
In line 1 but not in 2:15

Regards

Thanks! It seems to be working fine.