Compare files using Unix scripting

I have a file containing the below data obtained after running a diff command

> abc 10
< abc 15
> xyz 02
<xyz 05

.....

Does anyone know how i can obtain output like :

previous value of abc is 10 and present value is 15

similarly for all the comparisons in the text file

Could this help you ?

diff -b --side-by-side --suppress-common-lines file1 file2

The o/p printed is the result of the diff operation .

i need to print old values and new values of abc , xyz etc using that result . how do i do that

If your diff output is as simple as you posted, then try this:

awk '
    { gsub( "^.", "& " ); }   # needed if leading "<" isnt always separated by whitespace from rest of line
    /^</ {
        if( old[$2] )
            printf( "%s  old=\"%s\"  new=\"%s\"\n", $2, old[$2], $3 );
        else
            new[$2] = $3;
    }
    /^>/ {
        if( new[$2] )
            printf( "%s  old=\"%s\"  new=\"%s\"\n", $2, $3, new[$2] );
        else
            old[$2] = $3;
    }
'  input >output

Output should be something like:

abc  old="10"  new="15"
xyz  old="02"  new="05"

What if i'm readin the diff ouput from a file ?

Replace 'input' on the awk command with your file name. If the file is in diff.out, then

awk '

# lines of awk programme
' diff.out

I would want to redirect this output to file also .

printf( "%s  old=\"%s\"  new=\"%s\"\n", $2, $3, new[$2] )

Currently i'm getting an error like :

awk:cmd line:11 (filename=input.txt FNR=4) fatal:division by zero

attempted when i'm trying

printf( "%s  old=\"%s\"  new=\"%s\"\n", $2, $3, new[$2] ) > output.txt

in the snippet of code given

---------- Post updated 2012-09-25 at 01:56 AM ---------- Previous update was 2012-09-24 at 01:22 PM ----------

Any solutions ??

Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.

If this is truly in awk, you need > "filename.txt" with the filename in quotes.

But you haven't posted your entire code, so I have no idea why it's complaining about division by zero.

Thanks a lot !!! It worked .. Btw could you explain why i need to put in Quotes ???

Because if you don't, it will take it to be a variable name. awk's not shell. :slight_smile: