Input 2 files, calculate diffs line by line

Hi

I am new to Unix and need help with the following (to you all I'm sure) simple task. I am trying to output the differences between previous snaphots of various filesystem sizes to the present sizes.

I have three files (e.g.) :

file 1 file 2 file 3
10 100 /var
20 200 /opt
300 30 /tmp

I need a script to subtract the respective fields of file1 & file2, match this up with the respective fields in file3 and to output the result of this to standard output line by line.

i.e. Filesystem /var has changed by 90Kb
Filesystem /opt has changed by 180KB
Filesystem /tmp has changed by 270KB

I have tried :

#!/bin/ksh
for filesystem in `cat filesystems`
do
for curr_fs in `cat curr_fs_sizes`
do
for prev_fs in `cat prev_fs_sizes`
do
change=`expr ${curr_fs} - ${prev_fs}`
echo "Filesystem ${filesystem} has changed by ${change}Kb"
done
done
done

which cycles through every file 3 times each :frowning:

Any help greatly appreciated folks ! :frowning:

If I have understood your requirement properly, this is what you can do...

paste file1 file2 file3 > newfile
awk '{printf("Filesystem %s has changed by %d Kb.\n",$3,$2-$1)}' newfile

Not what I was expecting rakeshawasthi ! But it does of course work. Many thanks for taking the trouble to reply to me.

Assuming the given files have the same number of records, you can do it in one command,

awk '{ if((getline v1 < f1)>0 && (getline v2 < f2)>0)
       print "Filesystem " $0 " has changed by " (v1>v2?v1-v2:v2-v1)"Kb"
     } ' f1="./file1" f2="./file2" file3

Output:

Filesystem /var has changed by 90Kb
Filesystem /tmp has changed by 180Kb
Filesystem /opt has changed by 270Kb

yes that works too - thank you rubin