Add values in 2 columns and subtract from third


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add values in 2 columns and subtract from third
# 1  
Old 05-02-2013
Add values in 2 columns and subtract from third

Hi All,
I have a file with thousands of lines in the following format, where
Field1=First 8 characters
Field2-9-16 characters
Field3=17-26 characters

I need to add Field 1 and Field2 and subtract the result from Field 3.

Field3=Field3 - (Field1 + Field2)

Code:
0012.00 0010.00 0001576.53
0100.00 0500.00 0000952.83
0500.00 0500.00 0001760.74
0000.00 0000.00 0001536.04
0000.00 0000.00 0000389.08
0000.00 0000.00 0000942.09
0000.00 0000.00 0000884.26

Desired output file
Code:
0012.00 0010.00 0001554.53
0100.00 0500.00 0000352.83
0500.00 0500.00 0000760.74
0000.00 0000.00 0001536.04
0000.00 0000.00 0000389.08
0000.00 0000.00 0000942.09
0000.00 0000.00 0000884.26

Any help will be appreciated!
# 2  
Old 05-02-2013
Sounds like a task for awk. What have you tried so far? I can see you have plenty of threads opened with questions about similar problems, which have all been answered and solved.
Any post before the OP answers will be moderated.

Last edited by zaxxon; 05-02-2013 at 12:15 PM..
# 3  
Old 05-02-2013
This is what I have for now.

Code:
BEGIN {
out_filel="reports/tst3"

parta=substr($0,1,8)
partb=substr($0,9,8)
totalamt=substr($0,17,10)

totalamt=(totalamt)-(parta + partb) > out_file1

}

And this is how I am calling the awk program.

Code:
cat ./reports/tst2|awk -f ./awk_progs/test.awk

But the output file is not getting created.
# 4  
Old 05-02-2013
Try this one
Code:
awk '{print $1 " "$2 " " $3-($1+$2)}' file
0012.00 0010.00 1554.53
0100.00 0500.00 352.83
0500.00 0500.00 760.74
0000.00 0000.00 1536.04
0000.00 0000.00 389.08
0000.00 0000.00 942.09
0000.00 0000.00 884.26

This User Gave Thanks to Revansidhu For This Post:
# 5  
Old 05-02-2013
To preserve leading zeros in 3rd field:
Code:
awk '{$3=sprintf("%010.2f",$3-($1+$2))}1' file


Last edited by Yoda; 05-02-2013 at 01:48 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add values to file in 2 new columns

Columns 4 and 5 are X and Y coordinates, column 6 is the elevation I would like to add 2 new columns at the end of the file with values the distance between first(X)(Y) and last location (X)(Y), based in 2 rows the difference in elevation = ($6-prev6) How to calculate the requested values... (6 Replies)
Discussion started by: jiam912
6 Replies

2. Shell Programming and Scripting

awk to subtract from values in file

data.txt: 0,mq_conn_open_error,1444665949,734,/PROD/G/cicsitlp/sys/unikixmain.log,64K,mq_conn_open_error,62022,0,733--734 0,mq_conn_open_error,1444666249,734,/PROD/G/cicsitlp/sys/unikixmain.log,64K,mq_conn_open_error,62022,0,734--734... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Add new column which is subtract of 2 columns.

Hi below is a file Date Category Time Attempts Success 2/17/2014 PayFlow ATB 0.999988 4039104 4039057 2/18/2014 PayFlow ATB 0.999912 4620964 4620558 2/19/2014 PayFlow ATB 0.999991 4380836 4380796 2/20/2014 PayFlow ATB 0.999988 5031047 5030985 2/21/2014 ... (5 Replies)
Discussion started by: villain41
5 Replies

4. UNIX for Dummies Questions & Answers

awk to add/subtract an integer to/from each entry in columns?

---------- Post updated at 01:58 PM ---------- Previous update was at 01:48 PM ---------- For some reason my question is not getting printed. Here are the details: Greetings. I would like to add/subtact an integer to/from two columns of integers. I feel like this should be easy using awk... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

5. Shell Programming and Scripting

Add the values in second and third columns with group by on first column.

Hi All, I have a pipe seperated file. I need to add the values in second and third columns with group by on first column. MYFILE_28012012_1115|47|173.90 MYFILE_28012012_1115|4|0.00 MYFILE_28012012_1115|6|22.20 MYFILE_28012012_1116|47|173.90 MYFILE_28012012_1116|4|0.00... (3 Replies)
Discussion started by: angshuman
3 Replies

6. Shell Programming and Scripting

Help need to subtract the data from 2 columns

space_used.lst /dev/vx/dsk/A06487-S01-c4e3s-ORACLE-dg/oradata01 505G 318G 175G 65% /dborafiles/nethealth21/PV/oradata01 /dev/vx/dsk/A06487-S01-c4e3s-ORACLE-dg/oradata02 505G 433G 67G 87% /dborafiles/nethealth21/PV/oradata02 /dev/vx/dsk/A06487-S01-c4e3s-ORACLE-dg/oradata03 507G 422G 79G 85%... (4 Replies)
Discussion started by: sathik
4 Replies

7. Shell Programming and Scripting

for uniq entries add values in corresponding columns

Hi, I have a file as listed below.. What I want to get is for each unique value in column 1 the corresponding values in the rest of the columns should be summed up.. AAK1 0 1 0 11 AAK1 0 0 1 1 AAK1 0 0 1 2... (2 Replies)
Discussion started by: Diya123
2 Replies

8. Shell Programming and Scripting

How to subtract a number from all columns?

Hi, I want to subtract a number from all columns except the first column. I have a number of files each having different columns around 60/70. How to do that in awk or any other command? Thanks Input Col 1 Col 2 Col3 - - - - Col55 1 .0123 .098 - - - 0.6728 2 - -... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

9. Shell Programming and Scripting

Subtract field values

I've got a long logfile of the form network1:123:45:6789:01:234:56 network2:12:34:556:778:900:12 network3:... I've got a similar logfile from a week later with different values for each of the fields eg network1:130:50:6800:10:334:66 network2:18:40:600:800:999:20 network3:... ... (5 Replies)
Discussion started by: Yorkie99
5 Replies

10. Shell Programming and Scripting

how to flip values of two columns and add an extra column

Hi guys, Couldn't find the solution of this problem. Please Help! I have a file- Input_File TC200232 92 30 TC215306 2 74 TC210135 42 14 I want an output file in which if column2>column3, the values are swapped and an additional column with value Rev_Com is... (4 Replies)
Discussion started by: smriti_shridhar
4 Replies
Login or Register to Ask a Question