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;
File
Outputs
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:
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.
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
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.
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.
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)
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)
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)
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)
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)
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)
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)