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
# 1  
Old 05-23-2009
calculate the difference between numbers in lines of file

Hi everyone,

i have files containing lines of number:

109
107
67
62
..
..

i want to calculate the difference between numbers in lines 1 and 2, 3 and 4, 5 and 6 and so on. would someone help me please?. Thanks
# 2  
Old 05-23-2009
Try this
awk '{s=$0;getline;print s-$0;next}' file
# 3  
Old 05-23-2009
thanks rakeshawasthi,

it works, however i need the result to be in absolute value (always positive value). would you help me? thanks
# 4  
Old 05-23-2009
awk '{s=$0;getline;r=s-$0;if (r<0)r=-r;print r;next}' file
# 5  
Old 05-23-2009
thanks, it solves my problem perfectly.
i have one more question, how can i display only lines which the third field is different from the third field of previous line. for ex:

a/fruit/mango
a/fruit/mango
a/fruit/apple
a/fruit/apple
a/fruit/jackfruit

i need the result to be:
a/fruit/mango
a/fruit/apple
a/fruit/jackfruit

thanks, i really appreciate your help.
# 6  
Old 05-23-2009
Well thats easy.. if everything is going to be "a/fruit/fruitname" and you only want to do consecutive lines, then use the command "uniq". If you want to remove duplicate lines that are not consecutive then you can use "sort -u". Example(s):

Starting data: (added a few rows to help show how it works).
Code:
-bash-3.00$ cat test.txt
a/fruit/mango
a/fruit/mango
a/fruit/apple
a/fruit/apple
a/fruit/jackfruit
a/fruit/mango
a/fruit/apple
a/fruit/mango
a/fruit/mango

Using "uniq":
Code:
-bash-3.00$ uniq test.txt
a/fruit/mango
a/fruit/apple
a/fruit/jackfruit
a/fruit/mango
a/fruit/apple
a/fruit/mango

Using "sort -u"
Code:
-bash-3.00$ sort -u test.txt
a/fruit/apple
a/fruit/jackfruit
a/fruit/mango

# 7  
Old 05-24-2009
Thanks,
the complete example above is like this:
a/fruit/mango/1
a/fruit/mango/6
a/fruit/apple/3
a/fruit/apple/7
a/fruit/jackfruit/4
a/fruit/jackfruit/8

what i want is remove the previous line when its third field contains the same value as the third field of current line. so the result will be like this:
a/fruit/mango/6
a/fruit/apple/7
a/fruit/jackfruit/8

would you or someone help me please?
 
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