calculate the difference between numbers in lines of file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers calculate the difference between numbers in lines of file
# 8  
Old 05-24-2009
Code:
 awk -F/ '{a[$3]=$0;next}END{for(i in a) print a[i]}' filename


-Devaraj Takhellambam
# 9  
Old 05-24-2009
Quote:
Originally Posted by devtakh
Code:
 awk -F/ '{a[$3]=$0;next}END{for(i in a) print a[i]}' filename


-Devaraj Takhellambam
I don't think next is needed in there
# 10  
Old 05-24-2009
thanks devaraj, however it only gives me the last line of the file. would you explain about the code? thanks
# 11  
Old 05-24-2009
Quote:
Originally Posted by vidyadhar85
I don't think next is needed in there
You are right. next is not necessary here as it deals in the main loop with only one file.
# 12  
Old 05-24-2009
Quote:
Originally Posted by ahidayat
thanks devaraj, however it only gives me the last line of the file. would you explain about the code? thanks
What is your input like? The key to the above awk script is the third field. You may want to change it to suit your /p accordinlgy.
Code:
a[$3]=$0

# 13  
Old 05-24-2009
this is my input file:
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0000.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0001.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0010.bmp
Data\DiStAnCe\DistanceSource\130 m\s1\130-s1-0002.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0011.bmp
Data\DiStAnCe\DistanceSource\130 m\s1\130-s1-0003.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0012.bmp
Data\DiStAnCe\DistanceSource\130 m\s1\130-s1-0004.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0034.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0035.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0036.bmp
Data\DiStAnCe\DistanceSource\130 m\s1\130-s1-0031.bmp

what i want to do is remove the previous line where its third field is similar with the third field of current line. by the way, my final goal is calculate the difference of numbers in lines of that file and get the average. so the result i want will be:

Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0010.bmp
Data\DiStAnCe\DistanceSource\130 m\s1\130-s1-0002.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0011.bmp
Data\DiStAnCe\DistanceSource\130 m\s1\130-s1-0003.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0012.bmp
Data\DiStAnCe\DistanceSource\130 m\s1\130-s1-0004.bmp
Data\DiStAnCe\DistanceDestination\130 m\d1\130-d1-0036.bmp
Data\DiStAnCe\DistanceSource\130 m\s1\130-s1-0031.bmp

then ...

010
002
011
003
012
004
036
031

then

8 (010-002, first line minus second line)
8 (011-003)
8 (012-004)
5 (036-031)

then

7.25 (average of them), this is the final result

i've been messed around with this problem for three days, and still i can't get what i want. would you or someone help me please ?. thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Calculate time difference between two lines

i grepped the time stamp in a file as given below now i need to calculate time difference file data: 18:29:10 22:15:50 (5 Replies)
Discussion started by: vivekn
5 Replies

3. Shell Programming and Scripting

Comparing two files with numbers and taking difference in third file

Hi All, I have two files in the following format, with numbers being defined under columns(described by a set of headers) and rows(again defined by a set of identifiers) 2013 2013 Make200 Make201 Merc BMW Jpur Del ... (9 Replies)
Discussion started by: dev.devil.1983
9 Replies

4. Shell Programming and Scripting

Calculate date difference

Hi All Can you please help me with UNIX script code that will work with ksh shell on UNIX server My Requirement Time1: 09/17/13101536 Time2: 09/16/13101536 I want to calculate the difference in minutes for the dates with format given above. I have a requirement to wait for... (5 Replies)
Discussion started by: jimmyb
5 Replies

5. Shell Programming and Scripting

How to calculate difference:?

Experts, file1 : Want to find the difference of $3 field from next line's 3rd field, The difference to be calculated from next lines 3rd field, to current lines lines 3rd field. file1 : Jun24_2013.06242013 3301244928 3133059904 167370640 95% Jun25_1124.06252013 3301244928... (4 Replies)
Discussion started by: rveri
4 Replies

6. Shell Programming and Scripting

Calculate the mean of numbers

I have a text file which has 3 columns. The first two columns are shown below. 57 7 23 8 14 17 23 1 I need to print mean using the following formula . mean =sum of the numbers of $2/total numbers of $1 output:- sum of $2 = 33 total numbers = 4 mean= 8.25 (1 Reply)
Discussion started by: andreaalex
1 Replies

7. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

8. Shell Programming and Scripting

Calculate difference between consecutive data points in a column from a file

Hi, I have a file with one column data (sample below) and I am trying to write a shell script to calculate the difference between consecutive data valuse i.e Var = Ni -N(i-1) 0.3141 -3.6595 0.9171 5.2001 3.5331 3.7022 -6.1087 -5.1039 -9.8144 1.6516 -2.725 3.982 7.769 8.88 (5 Replies)
Discussion started by: malandisa
5 Replies

9. Shell Programming and Scripting

Calculate the time difference between a local file and a remote file.

I m stuck with a issue. I need to calculate the time difference between two files.. one on the local machine and one on the remote machine using a script. Can any one suggest the way this can be achevied Thanks, manohar (1 Reply)
Discussion started by: meetmano143
1 Replies

10. Shell Programming and Scripting

How to calculate the time difference...

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -2 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" |head -1 | awk '{print echo "PREVIOUS:-- Start Date&Time: " $3,$4,echo "|| End Date&Time:... (1 Reply)
Discussion started by: suri.tyson
1 Replies
Login or Register to Ask a Question