Running Differences Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running Differences Column
# 1  
Old 02-08-2010
Running Differences Column

Hello everyone,

I had such a helpful and quick response last time and it worked so perfectly, perhaps someone can help me with this problem I have (once again this is for research and not a homework problem). For instance, I have a file (varying numbers of rows, etc) with three columns of data in each row:

11 20 180
13 55 180
10 75 360
20 15 400

I would like a very small script (using awk preferably, but bash works too) that prints a fourth column that keeps track of the running difference between the value of the third column on each row. So, the output would look something like this:

11 20 180 0
13 55 180 0
10 75 360 180
20 15 400 220

The 4th column on the first row would be zero since it is the start of the file. The value for the 3rd column didn't change on the second row, so the 4th column is zero again for the second row. The value of the 3rd column changed by 180 from the second row to the third row, so 180 is printed. On the fourth row, the 3rd column value changed by 40, so that number is added to 180 from the 4th row of the 3rd column, and 220 is printed. This needs to continue on down until the end of the file.

Any and all help would be most appreciated. Thank you very much for helping me further my computer programming learning skills!
# 2  
Old 02-08-2010
Try this:

Code:
awk -va=0 -vb=0 '{if (NR!=1) {b=b+$NF-a}; if (NR==1) {print $0,b} else {print $0,b}; a=$NF;}' file.txt



---------- Post updated at 06:19 PM ---------- Previous update was at 06:17 PM ----------

I just saw that the if statement is actually redundant, so this makes more sense:

Code:
awk -va=0 -vb=0 '{if (NR!=1) {b=b+$NF-a};  {print $0,b}; a=$NF}' file.txt

# 3  
Old 02-08-2010
Thanks for the help with the code! It does exactly what I want it to. I appreciate the assistance!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in awk: running a loop with one column and segregate data 4 each uniq value in that field

Hi All, I have a file like this(having 2 column). Column 1: like a,b,c.... Column 2: having numbers. I want to segregate those numbers based on column 1. Example: file. a 5 b 9 b 620 a 710 b 230 a 330 b 1910 (4 Replies)
Discussion started by: Raza Ali
4 Replies

2. UNIX for Dummies Questions & Answers

Extracting combined differences based on a single column

Dear All, I have two sets of files. File 1 can be any number between 1 and 20 followed by a frequency of that number in a give documents... the lines in the file will be dependent to the analysed document. e.g. file1 1,5 4,1 then I have file two which is basicall same numbers but with... (2 Replies)
Discussion started by: A-V
2 Replies

3. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

4. Shell Programming and Scripting

Read column and find differences...

I have this file 427 A C A/C 12 436 G C G/C 12 445 C T C/T 12 447 A G A/G 9 451 T C T/C 5 456 A G A/G 12 493 G A G/A 12 I wanted to read the first column and find all other ids which are differences less than 10. 427 A C A/C 12 436 436 G C G/C 12 427,445... (7 Replies)
Discussion started by: empyrean
7 Replies

5. Shell Programming and Scripting

{} and ( ) differences

Can u tell the diff between the 1) $a and ${a} 2)] and ( ) 3)" " and ' ' , ` ` 4) 'a' , "a", please explain with simple example (1 Reply)
Discussion started by: mrbinoy
1 Replies

6. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

7. Shell Programming and Scripting

Differences between 2 Flat Files and process the differences

Hi Hope you are having a great weeknd !! I had a question and need your expertise for this : I have 2 files File1 & File2(of same structure) which I need to compare on some columns. I need to find the values which are there in File2 but not in File 1 and put the Differences in another file... (5 Replies)
Discussion started by: newbie_8398
5 Replies

8. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

9. UNIX for Dummies Questions & Answers

Compare 2 files for a single column and output differences

Hi, I have a column in 2 different files which i want to compare, and output the results to a different file. The columns are in different positions in those 2 files. File 1 the column is in position 10-15 File 2 the column is in position 15-20 Please advise Thanks (1 Reply)
Discussion started by: samit_9999
1 Replies

10. Shell Programming and Scripting

Report running processes in a specific format (tricky column filtering)

First off I have HP Unix, so as far as I can tell the PS command does not let me list specifically which columns I would like or what order I would like to see them. I want to make a custom PS script for checking what SAS process are running. I want it to take 1 argument of user name and only... (6 Replies)
Discussion started by: goldfish
6 Replies
Login or Register to Ask a Question