compare files and calculating


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare files and calculating
# 1  
Old 01-25-2007
compare files and calculating

Hi everybody:
Could anybody tell me how can I do this task in AWK language.
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
.............................
.............................


Thanls in advance and cheers. Smilie
tonet
# 2  
Old 01-25-2007
Code:
tail -1 file1 | read x y
head -1 file2 | read a b
if [ $a -eq $x ]
then
    diff=$( echo $b - $y | bc )
    awk -v dif=$diff ' { print $1 , ( $2 - dif ) } ' file2
fi

# 3  
Old 01-25-2007
Thanks Ambu23, but Ive tried to use it and I don know why, it appears this error:
./gir_int.sh: line 72: bc: command not found
I hope that it is because this command are not installed (?).
What do you recommend me ?. Smilie
tonet
# 4  
Old 01-25-2007
Code:
tail -1 file1 | read x y
head -1 file2 | read a b
if [ $a -eq $x ]
then
    awk -v var1=$b -v var2=$y ' BEGIN { dif=var1 - var2 } { print $1 , ( $2 - dif ) } ' file2
fi

# 5  
Old 01-25-2007
Thanks again.
Now I've got an ouput file like:

19.83 217.83
28.67 231.34
42.74 264.576
65 232.5
100 210

but the second column does not correspond with the difference.
I did this (in my case):

tail -1 05080300.ddr | read x y z
head -1 mls_65.tmp1 | read a b c
if [ $a -eq $x ]
then
LC_ALL=en_DK awk -v var1=$b -v var2=$y ' BEGIN { dif=var1-var2 } { print $1, ($2-dif) } ' mls_65.tmp1 > mls_65.tmp2 #file2
fi

Really my files it has got three columns. Smilie
tonet
# 6  
Old 01-26-2007
Try...
Code:
$ awk 'FNR==NR{x=$2;next}FNR==1{d=$2-x;$2=x}FNR>1{$2-=d}1' file1 file2
19.83 214.468
28.67 227.978
42.74 261.214

# 7  
Old 01-26-2007
Thanks a lot Ygor. It works correctly. Cheers Smilie
tonet
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Calculating how many sub directories and files in 'documents/'

I'm learning Linux through a gameshell. Like the title says; I need to find the amount of sub directories and files in 'documents/'. The command runs fine but its the not the right answer. Here's my command : find docuemnts/ -type f | wc -l | awk '{print $1}' What am i doing wrong ? (2 Replies)
Discussion started by: forzatekk
2 Replies

2. Shell Programming and Scripting

Calculating average from files

I have some files with the following contents.I would like to calculate average of fifth column. How can I do this with awk? file1 cat 95.9 152 78.0 17.9 rat 67.1 153 36.5 30.6 dog 81.4 154 68.1 13.3 dog 92.0 155 55.5 36.5 rat 73.8 156 23.9 49.9 file2 rat... (4 Replies)
Discussion started by: avina
4 Replies

3. Shell Programming and Scripting

Performance of calculating total number of matching records in multiple files

Hello Friends, I've been trying to calculate total number of a certain match in multiple data records files (DRs). Let say I have a daily created folders for each day since the beginning of july like the following drwxrwxrwx 2 mmsuper med 65536 Jul 1 23:59 20150701 drwxrwxrwx 2 mmsuper... (1 Reply)
Discussion started by: EAGL€
1 Replies

4. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

5. Emergency UNIX and Linux Support

Calculating total space in GB for all files with typical pattern

Hi Experts, In a particular dir, I have many files *AJAY*. How can I get total size of all such files. I tried du -hs *AJAY* but it gave me individual size of all files. All I require is summation of all. Thanks, Ajay (4 Replies)
Discussion started by: ajaypatil_am
4 Replies

6. Shell Programming and Scripting

calculating column summation in a directory of flat files

Hello Guru s I need your kind help to solve my below issue I have a directory of flat files and have to calculate sum of some columns from the flat file . Say for flat file 302 I need the column summation of 2 and 3 rd column For flat file 303 I need the column summation of 5 and... (2 Replies)
Discussion started by: Pratik4891
2 Replies

7. Shell Programming and Scripting

Calculating average of 100 different files of same size

Hey guys..... I have many files (lets say 100 or more) of same size, and I want to create a new output file and calculate the average of first row fifth column in all files and print it in first row of output file, then 2nd row fifth col in all 100 files and print it in 2nd row of output... (1 Reply)
Discussion started by: CAch
1 Replies

8. UNIX and Linux Applications

sqlite: calculating with dates - compare current date minus 6 months with stored record

Hi I have a table with name, date in format DD.MM.YYYY. I need to something like this (I try to explain in pseudo code) if SYSDATE (current date) minus 6 months > $expiry date print OK else print NOK with $name and $expiry date I know this is possible with Oracle. How to do this... (0 Replies)
Discussion started by: slashdotweenie
0 Replies

9. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

10. Shell Programming and Scripting

Help with calculating size of files.

Hi All, I am very new to shell scripting. I have a text file which is the output of another utility. This text file has a list of SAF files which is basically a list of orphan attachments in the attachments directory. Now I want to read each file name from the file, get its size and repeat this... (6 Replies)
Discussion started by: RajeshReddy
6 Replies
Login or Register to Ask a Question