Operating System - HP-UX
1821244 Members
2791 Online
109632 Solutions
New Discussion юеВ

Awk problem - convert a char string to integer

 
SOLVED
Go to solution
Ronelle van Niekerk
Regular Advisor

Awk problem - convert a char string to integer

I'm trying to total up the size of a backup session using the omnirpt command.
Unfortunately the Size field is not constant if using " " as a delimiter because the Description field sometimes has spaces in it.

So I've grabbed the size like so:
used_kb = substr($0,123,10)
But that inserts spaces in front of the number.

So I tried getting rid of them with:
gsub(" ","",used_kb)
and it prints fine but when I total all the lines with:
total_kb = total_kb + used_kb
I get funny output:
TOTAL KB = 1.25552e+07

Is there a way to convert a line that looks something like this:
123
to an integer?

-Ronelle
rm -r /it/managers
3 REPLIES 3
Hein van den Heuvel
Honored Contributor
Solution

Re: Awk problem - convert a char string to integer

You don't suppose that the output is simply correct perhaps? 1e07 kb would be 10,000,000 kb = 10,000 mb = 10 gb. Not unreasonable perhaps?

If the output is correct, you only have an output formatting problem. Use PRINTF to force your own method over the default.
fwiw... I sometimes use 0 + string to force an number conversion... 'just to be sure'.

# awk 'BEGIN{u=substr(" 123 ",2,6); t=u+0;print u "|" t "|"}'
123 |123|
# awk 'BEGIN{t=12345678;print t; printf ("%9d\n",t)}'
1.23457e+07
12345678

met vriendelijke groetjes,
Hein.
Hein van den Heuvel
Honored Contributor

Re: Awk problem - convert a char string to integer

arghh 1:
The forum stripped my carefully places spaces, I chould have also printed a leading seperation bar "|", but I think it also converted multiple spaces before and after the " 123 " into a single space.

arghh 2: (minor), I just check Ronelle's profile and while the name suggests Holland, to registered country is New Zealand. Oh well, so much for the localized sign off. Then again...

Cheers,
Hein.

Ronelle van Niekerk
Regular Advisor

Re: Awk problem - convert a char string to integer

Thanks for the info Hein.

I'm actually from South Africa and speak afrikaans so your friendly greeting didn't go un-noticed!
rm -r /it/managers