awk - Adding and Subtracting Numbers from 2 Columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Adding and Subtracting Numbers from 2 Columns
# 1  
Old 09-01-2016
awk - Adding and Subtracting Numbers from 2 Columns

Hi Folks,

I have a file with 2 columns TAB delimited and I want to add '1' to the first column and subtract '-1' from the second column.

What I have tried so far is;

Code:
awk -F"\t" '{ $1-=1;$2+=1}1' OFS='\t' file

File

Code:
0623	0623
0624	0624
0643	0643
1059	1037
1037	1037
1038	1038
1056	1159

Outputs

Code:
622	624
623	625
642	644
1058	1038
1036	1038
1037	1039
1055	1160

This works, as in it will add and subtract as requested. However, AWK is removing the starting digit 0 on the output. I need to keep this as my script is adding and subtracting time.

The two issues I am facing is AWK removing the starting 0 and also if I add '1' to the "time" 1159 my way of counting time will output 1160, where as I would like it to read 1200.

Any help would be met would the greatest of appreciation.

Thank you


Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by pshields1984; 09-01-2016 at 08:36 AM.. Reason: Changed QUOTE to CODE tags.
# 2  
Old 09-01-2016
awk isn't that easy in respect to either of your questions.
- if "expr can be represented ... as an exact integer then it is converted to sprintf("%d", expr)." (cf man awk) So you can't use OFMT or CONVFMT immediately, you need to play dirty tricks with them or use formatted printing of each field.
- if you want a "carry flag" to apply at a value of 60, again you need to take the scenic route.

How about making the values slightly deviate from an integer so the CONVFMT applies, and to use a conditional assignment for the carry over? Try
Code:
awk -F"\t" '{ $1-=1.001;$2+=1.001;$2+=($2%100>=60)?40:0}1' OFS='\t' CONVFMT="%04.0f"  file
0622	0624
0623	0625
0642	0644
1058	1038
1036	1038
1037	1039
1055	1200

Please be aware of the non-integer values if you need to use $1 and $2 in further arithmetics.
The "carry under" for $1 is left as an exercise to the reader. Of course the operations on every single field can be combined into one statement.

Last edited by RudiC; 09-01-2016 at 09:20 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-01-2016
Thanks RudiC, I love how your brain went there to "trick" CONVFMT and I never knew a carry flag. I have tried your suggestion and it works brilliantly. My input file works in tandem with your suggestion and always will. Many Many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

2. Shell Programming and Scripting

Adding/ Subtracting from Date

Hi , How can I add/substruct x number of days with date? For example My_Date=`date` Now I need Hope it's clear. (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

3. Shell Programming and Scripting

Searching columns and subtracting values in awk

Hi everyone, I had a similar question a couple days ago but my problem has gotten significantly (to me anyway) more complex. I have two files: File 1: 0808 166 166 62 9 0 1000fights 1 1 2 1 0 100places2visit 2 2 2 2 0 10veronica91 167 167 3 1 0 11thgorgeous 346 346 3806 1461 122... (2 Replies)
Discussion started by: collards
2 Replies

4. UNIX for Dummies Questions & Answers

Static date adding and subtracting years

Hi Gurus! I have a static date in a YYYYMMDD format; and I want get the date 2 years in the past and 2 years in the future. static_date=20010203 old_date=$static_date - 3 years future_date=$static_date + 2 years I was only able to research on dates that are current and not on static... (3 Replies)
Discussion started by: kokoro
3 Replies

5. Shell Programming and Scripting

Adding A Bump of Numbers with Awk

ok, so i have a bunch of numbers in a file that i'd like to add up. i dont know how to do it. This is how far i've gotten: echo "4 4 5 4 3 4 3 3 4 2 43 3 293 49 23" | sed 's/ / + /g' | awk -F" " I dont want to use the expr command with this as i dont trust it. any advice? thanks (1 Reply)
Discussion started by: SkySmart
1 Replies

6. Windows & DOS: Issues & Discussions

Adding or subtracting days from current date in batch script

Hi, I'm writing an batch file to create report In the batch file iam passing two arguments:startdate and finishdate Ex: startdate=07-sep-2009 finishdate=07-sep-2011 I need to have script that takes command line argument as input and gives me out currentdate last year and current date... (2 Replies)
Discussion started by: anand1773
2 Replies

7. Shell Programming and Scripting

Subtracting columns against each other

Hi All, I have a file of 100 lines of each having 1000 columns. I need to find the difference of each column against each other. That means, Col1-Col1; Col1-Col2; Col1-Col3;......Col1-Col1000; Col2-Col1; Col2-Col2; Col2-Col3;.... and so on ....up to Col1000-Col1000. Lets say the file is... (6 Replies)
Discussion started by: Fredrick
6 Replies

8. Shell Programming and Scripting

awk adding columns from different files

Hi, I have two files and I need to add column 3 of file1 to column 3 of file 2 > file3 I also need to repeat for column 4. Thanks (1 Reply)
Discussion started by: dsstamps
1 Replies

9. Shell Programming and Scripting

comparing files - adding/subtracting/formating columns

I have two files: file1.txt: FS Total Used Free Used% /u01 10000 8000 2000 80% /u02 10000 8000 2000 80% /u03 10000 8000 2000 80% /u04 10000 8000 2000 80% /u05 10000 8000 2000 80% /u06 10000 8000 2000 80% /u07 10000 8000 2000 80% /u10 10000 5000 5000 50% file2.txt:... (7 Replies)
Discussion started by: oabdalla
7 Replies

10. UNIX for Dummies Questions & Answers

adding or subtracting days in the o/p of date

how can we add or subtract days from the output of date command in unix... like if i want to subtract a day from the result of date command like this.. v_date=`date +%Y%m%d` this wud give me 20080519 now i want to subtract one day from this.. so tht it wud give me 20080518.. how do i do... (1 Reply)
Discussion started by: St.Fartatric
1 Replies
Login or Register to Ask a Question