1834936 Members
2281 Online
110071 Solutions
New Discussion

Re: awk script

 
skravi17
Advisor

awk script

Hi

This is my script

ls -l *.t | awk '{ print x +=$5 }'

it should add all the bytes for *.t files.
but i am getting 2.11362e+08. how to get
correct bytes.
11 REPLIES 11
steven Burgess_2
Honored Contributor

Re: awk script

Hi

The output is 200 meg

Regards

Steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: awk script

Hi

Another way of doing it would be to

ls -l | awk '{ print $5 } > /tmp/totals

Then add the totals in the file with

cat /tmp/totals | awk 'BEGIN {total=0;} {total=total+$1;} END {print total,"\n";}'

Regards

Steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: awk script

Hi again

Sorry, the correct value is 211,362,000

2^8

Regards

Steve
take your time and think things through
S.K. Chan
Honored Contributor

Re: awk script

Use the printf function within awk .. for example ..

# ls -l *.t | awk '{ printf ("%10.2f\n",x+=$5) }'

That will give you 10 digits before the decimel point and 2 digits after the decimel point.
skravi17
Advisor

Re: awk script

Hi
Thanks steven and skchan. skchan script
rectified my problem.
steven Burgess_2
Honored Contributor

Re: awk script

Hi

I see you are a new forum user

You can assign points by clicking on the 'assign points' tab next to replies

Regards

Steve
take your time and think things through
Nagarathinam
Advisor

Re: awk script

Just multiply the fraction by 100.

Naga
Nagarathinam
Advisor

Re: awk script

The result obtained is in MBs.

Naga
H.Merijn Brand (procura
Honored Contributor

Re: awk script

# perl -le '$s+=-s for<*.t>;print$s'

or reformatted to your liking

perl -le '$s+=-s for<*.t>;printf"%8.2f",$s'
Enjoy, Have FUN! H.Merijn
Shannon Petry
Honored Contributor

Re: awk script

Only one person pointed out the correct solution, but didnt explain the problem.

Problem: printing num E
This is the behavior of the print statement in awk.

Solution: use printf
printf() syntax is exactly like in C programming.

Regards,
Shannon
Microsoft. When do you want a virus today?
Klaus Crusius
Trusted Contributor

Re: awk script

Hi,


unfortunately awk uses internally a floating point arithmetic. That is less accurate than using integer arithmitic with same variable size, because some of the bytes are used for the exponent.

Regards, Klaus
There is a live before death!