Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

columns and rows processing add 2

Status
Not open for further replies.

Yarka

Technical User
Jan 14, 2007
192
ES
Hi,
How can I add a same number to a column using awk?
For example,
If I have a file as:
1.1 1.1 1.1 1.1
2 2 2 2
3.1 4.2 5 6
I need to add 0.01 on third column. My output must be:
1.1 1.1 1.11 1.1
2 2 2.01 2
3.1 4.2 5.01 6

And how can I add, for example 2 columns and its result put in
last position. Above example before, if want to add column 2 and 3, and put its result in last position of each row. How I can do it?

Thanks.
 
For the first question:

[tt]awk '{$3+=0.01; print}'[/tt]

For the second question:

[tt]awk '{print $0,$2+$3}'[/tt]

Annihilannic.
 
Thanks, for the answer.

But when I do awk '{$i+=0.01; print}', it doesn't write all decimals. Values are round. How can I have all decimals?

Thanks.
 
What operating system are you on? And what version of awk? When I run it I get exactly the output you requested. Perhaps try nawk if it is present.

Annihilannic.
 
Hi,
When I add numbers with 6 decimals, my output is 5 decimals. I need have all decimals. How can I do it?

For example:
a=0.001735 and b=0.001009, when i add it doing awk '{print $0,$2+$3}' my output is 0.00274 and not 0.002744.

My version of awk is GNU Awk 3.1.4. Nawk isn't present.

Thanks.
 
And what about this ?
awk '{printf "%s %.6f\n",$0,$2+$3}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top