Calculating differences line by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculating differences line by line
# 8  
Old 03-12-2013
is there anyway to hardcode the following scenario into the code?

for instance, one line has a value of:

Code:
0.00476979

the next line has a value of:

Code:
-0.0142208

the percentage difference is =
Code:
133.541%

notice it is positive. should be a negative, considering the numbers dropped.

also, what if both numbers are negative:


Code:
-0.0285025

and

Code:
-0.0477696

the percentage difference is:
Code:
40.3334%

should be
Code:
-40.3334%

bipinajith posted a code earlier that can do this. but is there a way to make awk do the proper calculation automatically?

basically if its a drop, the percentages should be negative. if it's an increase, the percentages should be positive.
# 9  
Old 03-12-2013
Code:
awk -F"[=,]" '{prev=$4; print; getline; print $0","($4-prev)/($4<0?-$4:$4)*100"%"}' input

The highlighted part basically does an absolute value of $4.
Note that you'll get into problems if the value is zero (division by zero).

Last edited by mirni; 03-12-2013 at 05:57 PM..
This User Gave Thanks to mirni For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Shell Programming and Scripting

How to make diff show differences one line at a time and not group them?

Is there a way to tell diff to show differences one line at a time and not to group them? For example, I have two files: file1: line 1 line 2 line 3 diff line 4 diff line 5 diff line 6 line 7 file2: line 1 line 2 line 3 diff. line 4 diff. line 5 diff. line 6 line 7 (13 Replies)
Discussion started by: mmr11408
13 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies
Login or Register to Ask a Question