Difference between corresponding elements of successive rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between corresponding elements of successive rows
# 1  
Old 03-24-2011
Difference between corresponding elements of successive rows

Hi,

I have a file in the following format
Code:
a1 b1 c1 d1 
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

I need a script to find the difference between corresponding values of successive rows. So the output would have one less row than the input file and should look like:
Code:
a2-a1 b2-b1 c2-c1 d2-d1
a3-a2 b3-b2 c3-c2 d3-d2
a4-a3 b4-b3 c4-c3 d4-d3

Thanks

Last edited by Franklin52; 03-24-2011 at 04:08 AM.. Reason: Please use code tags
# 2  
Old 03-24-2011
Code:
tac |awk 'NR==1{l1=$1;l2=$2;l3=$3;l4=$4}NR>1{print l1"-"$1,l2"-"$2,l3"-"$3,l4"-"$4;l1=$1;l2=$2;l3=$3;l4=$4}'|tac

# 3  
Old 03-24-2011
I would like to perform the mathematical operation (subtract) rather than just printing them, i.e. it should print the "actual" values of a2-a1 b2-b1 c2-c1 d2-d1 and so on for rest of the rows.
# 4  
Old 03-24-2011
Quote:
Originally Posted by sajal.bhatia
I would like to perform the mathematical operation (subtract) rather than just printing them, i.e. it should print the "actual" values of a2-a1 b2-b1 c2-c1 d2-d1 and so on for rest of the rows.
Hi sajal.bhatia,
Try with:
Code:
echo "10 20 30 40
15 30 42 51
18 32 48 60
25 34 50 71" | awk '{
a[NR]=$1;b[NR]=$2;c[NR]=$3;d[NR]=$4
}
END{
for(i=2;i<=NR;i++) 
print a[i]-a[i-1],b[i]-b[i-1],c[i]-c[i-1],d[i]-d[i-1]
}'
5 10 12 11
3 2 6 9
7 2 2 11

Best regards
# 5  
Old 03-24-2011
Quote:
Originally Posted by sajal.bhatia
I would like to perform the mathematical operation (subtract) rather than just printing them, i.e. it should print the "actual" values of a2-a1 b2-b1 c2-c1 d2-d1 and so on for rest of the rows.
Another approach:
Code:
awk '
NR==1{a=$1; b=$2; c=$3; d=$4; next}
{print $1-a, $2-b, $3-c, $4-d; a=$1; b=$2; c=$3; d=$4}
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

Calculating Time difference Between two Rows in Linux

16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. working in media company on a project OS: RHEl7 tried command: awk 'function... (2 Replies)
Discussion started by: vivekn
2 Replies

3. UNIX for Dummies Questions & Answers

Finding difference in 1st field for rows of data

I have a file that has multiple lines, of grouped data, that typically all have the same values in the 1st field, however, I would like to search the 1st field for any differences and set a flag to use in an "if" statement to run some other routine. An example of the typical file is below,... (2 Replies)
Discussion started by: co21ss
2 Replies

4. Shell Programming and Scripting

Sum from successive lines following date header to create data for graphing connections

Hi, I have a log file created from a load balancer showing connections to each member of a two member pool with the following format (where first field is source IP, second field is load balanced IP address and third field is destination member. I need to plot a graph by date/time and number of... (5 Replies)
Discussion started by: shog63
5 Replies

5. Shell Programming and Scripting

Adding successive values in awk

Hi, I have a large log file in the following format. Epoch-Time Bytes 899726401 20 899726401 30 899726402 40 899726402 10 899726402 50 899726403 50 899726403 ... (8 Replies)
Discussion started by: sajal.bhatia
8 Replies

6. Shell Programming and Scripting

Calculate difference between two successive values

Hi, I have a file containing timestamps (at micro-seconds granularity). It looks like the following: 06:49:42.383818 06:49:42.390190 06:49:42.392308 06:49:42.392712 06:49:42.393437 06:49:42.393960 06:49:42.402115 Now I need a sed/awk script to take the difference of two successive... (2 Replies)
Discussion started by: sajal.bhatia
2 Replies

7. Shell Programming and Scripting

Extract difference of two columns from different rows

Hello guys, Please help me to solve this problem. I have tried some awk commands but couldn't succeed. I have a tab delimited file where each record is separated by ------ and 4th column of each record is same. <INPUT FILE> ------ peon 53931587 53931821 ... (12 Replies)
Discussion started by: sam_2921
12 Replies

8. Shell Programming and Scripting

successive file renaming & overwriting

Hi all, In our webserver customer wants to see the latest 10 files arrived. So the names are hardcoded in webpage like : filename_01.txt filename_02.txt .... .... filename_10.txt where the filename_01.txt is the latest one (typically the output of ls -1t) in the /../webpage directory.... (2 Replies)
Discussion started by: sabyasm
2 Replies

9. Shell Programming and Scripting

Difference between two rows

Dears, I have a list as follows, 2 4 8 If I want to find the difference between two consecutive rows. Then I have to store the specific rows in two variables and then find the difference. Could someone tell how this can be done. Regards, (7 Replies)
Discussion started by: JimJim
7 Replies
Login or Register to Ask a Question