Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare files and calculating

Status
Not open for further replies.

tonivm

Technical User
Mar 2, 2006
64
ES
Hi everybody:
Could anybody tell me how can I do this.
I've two files (file1 & file2) where file1 its last row it has first field value equal that the first row in file2, thyen I want calculate the difference in other fields, and this values apply in file2. This is:

file1:
.............................
.............................
10.87 228.983
14.31 211.306
19.83 214.468

file2 :
19.83 217.83
28.67 231.34
42.74 264.576
.............................
.............................

Then diff=$2(file2)-$2(file1)=217.83-214.468=3.362, then I would like to have this output:
19.83 214.468
28.67 231.34-diff
42.74 264.576-diff
.............................
.............................

This is:
19.83 214.468
28.67 227.978
42.74 261.214
.............................
.............................


Thanks in advance and cheers. :)
 
And what have you tried so far ?
Tip: sometimes FNR is not the same as NR

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I tried this:

tail -1 file1 | read x y z
head -1 file2 | read a b c
if [ $a -eq $x ]
then
awk -v var1=$b -v var2=$y ' BEGIN { dif=var1 - var2 } { print $1 , ( $2 - dif ) } ' file2 > file3
fi

But I have:

19.83 217.83
28.67 231.34
42.74 264.576
65 232.5
100 210

and the second column does not correspond about I wish. :)
 
Are you aware that a single awk program may process many input files ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV:
Really I'm not sure. But I wonder that before use awk, if I save this:

sound=(`tail -1 05080300.ddr`);
declare -a bcn;
bcn=(`head -1 mls_65.tmp1`);
declare -a sound;

after I can use it into awk script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top