Previous Column Value (addition)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Previous Column Value (addition)
# 1  
Old 01-19-2010
Previous Column Value (addition)

Good day,

First off, I would just like to say that I've been helped by a lot of the posts here in my own scripting work in the past few months. I've been a long time roamer, but first time poster. That being said, I'm stuck trying to figure out this issue (For computational research, not a homework problem).

The problem seems pretty simple: I have a file with two columns :

1234 2
1568 4
6895 6

What I'd like to do is take the value of the second column and add it to the previous value of the second column in the previous row, and print that as a third column. So, the output would look like this;

1234 2 2
1568 4 6
6895 5 11

The third column of the first row would be 2 (since a "previous" column does not exist), then the third column of the second row would be 4 (value of second column in second row) + 2 (value of the third column in previous row) = 6.

It would then go on to add 5 + 6 and put that in the third row of the third column, and so on and so forth down the list. The value of the first column does not matter for this problem, and the number of rows in the file would vary depending on what I'm studying.

I have attempted using awk (even dabbled some in arrays), but to no avail, and I've tried a simple bash shell script but I got hopeless stuck with for loops. I hope I made this as easy as possible to understand, and any help would be much appreciated!
# 2  
Old 01-19-2010
Try this:
Code:
awk '{$3=$2+t;t+=$2}1' file

even shorter:

Code:
awk '{$3=t+=$2}1' file


Last edited by Franklin52; 01-19-2010 at 03:54 PM.. Reason: adding 2nd command
# 3  
Old 01-19-2010
Quote:
Originally Posted by Franklin52
Try this:
Code:
awk '{$3=$2+t;t+=$2}1' file

Simple, elegant, and worked like a charm. Thank you so much for you help, it was a life saver!
# 4  
Old 01-19-2010
Hello Franklin52,
Can you explain the purpose of "1" in your script?
Code:
awk '{$3=t+=$2}1' file

Hum... nevermind ^^
I figured out:
awk '1' file == cat file

Last edited by tukuyomi; 01-19-2010 at 04:31 PM.. Reason: Formating
# 5  
Old 01-19-2010
Quote:
Originally Posted by tukuyomi
Hello Franklin52,
Can you explain the purpose of "1" if your script?
Code:
awk '{$3=t+=$2}1' file

awk evaluates the 1 after the brace as true (not 0 or not empty) and the prints the current line by default, it's equivalent to {print}.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Group/concatenate certain column and basis on this do addition on other column

Hi Experts, Need your support I want to group/concatenate column 1,2,12 and 13 and if found duplicate then need to sum value of column 17,20,21 and column22. After concatenation if found unique then no action to be taken. Secondly want to make duplicate rows basis on grouping/concatenation of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Fill column from previous row

Hi, I have the following content in file ABBR DESC COL3 COL4 COL5 COL6 AAA text desc aaa text text text text text text text text text text text text BBB text desc bbb text text text text text text text text CCC ... (10 Replies)
Discussion started by: bobbygsk
10 Replies

3. Shell Programming and Scripting

Get previous value in column

Gents, Please can you help. Using the code below awk '{ if($1 < prev -50) {printf ("%4d then %4d \n",prev,$1) } ; prev = $1}' file1 I got 726 then 645 But my desired output is 644 then 726 then 645 Here is the file1 637 3007754557 10980 170800 638 3051154569 10764 170823... (8 Replies)
Discussion started by: jiam912
8 Replies

4. Shell Programming and Scripting

Fill data in column with previous value

Gents, Kindly help me. I have a file with empty values in selected column, I will like to fill the empty values with the previous value. Example Input file X 4959 30010 66727.00 20457.001 1 1441 66512.00 20234.00 20520.001 X 4959 30010 66727.00 20457.001 145 ... (7 Replies)
Discussion started by: jiam912
7 Replies

5. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

6. Shell Programming and Scripting

Replace column by random number addition

Here is my problem:- I have a file with pipe separated values. CR|20121021|079|ABC|N|DLS|00038|DLS|04750|1330597704|634234|634|0 CR|20121021|079|ABC|N|DLS|00038|DLS|05118|2071690102|354|351|3 CR|20121021|079|ABC|N|DLS|00038|DLS|05140|960051505|1088|1088|0... (4 Replies)
Discussion started by: Yoda
4 Replies

7. Shell Programming and Scripting

Read column values from previous and next line using awk

Hi, I have a csv file which contains data that looks something like this: Key1 Key2 Key3 New_Key1 New_Key2 New_Key3 102 30 0 - - - 102 40 1 30 40 50 102 50 2 40 50 ... (4 Replies)
Discussion started by: Nishi_Licious
4 Replies

8. Shell Programming and Scripting

Help with figuring division and addition based on column data and line numbers

I have a data file in the format of 1234 xxx 1234 xxx 1234 xxx 1234 xxxI want to be able to calculate the following - COLUMN1+((LINENUMBER-1)/365) The output needs to preserve the 2nd column - 1234 xxx 1234.00274 xxx 1234.00548 xxx What is the best way to do this? I am somewhat... (9 Replies)
Discussion started by: ncwxpanther
9 Replies

9. Shell Programming and Scripting

eAdd two fields in a column if their previous field values are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output file as follows If field3 in file1 is same as field3 in the next line then the field4 should add... (1 Reply)
Discussion started by: yerruhari
1 Replies

10. UNIX for Dummies Questions & Answers

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies
Login or Register to Ask a Question