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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math operations in awk statement

Status
Not open for further replies.

amkipnis

Programmer
Apr 15, 2003
21
US
Greetings,
When I try to execute the following awk command I am extracting the third column (size value) from the file which is given in Bytes (by default). I am getting error while trying calculate total size for all file's records and displaing them in the readable range (Bytes, KB, MB, GB, TB, etc...). I noticed that I am getting error when try to take exponential 10^length(totalsize).

Is there any better way to accomplish this ?

I appreciate for the help!
Thanks.
Alex.

grep 'Size' ${LOGDIR}${BU}_dasd_usage.txt | awk '{ totalsize += $3 } END {print "totalsize value: " totalsize/(10^length(totalsize)) "\n"}'
 
Any chance you could post the error message ?
If syntax error you can try use either nawk instead of awk or ** operator instead of ^.
Anayway you don't need grep:
nawk '/Size/{ totalsize += $3 } END {print "totalsize value: " totalsize/(10^length(totalsize)) "\n"}' ${LOGDIR}${BU}_dasd_usage.txt

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thank you PHV, it worked.

Here is an original error message:

awk: syntax error near line 1
awk: illegal statement near line 1
 
This will print the size in bytes or kilobytes or megabytes, etc.:
Code:
END{
  while (totalsize>=1024)
  { totalsize /= 1024
    i++
  }
  split("bytes KB MB GB TB", a)
  print totalsize, a[i+1]
}
 
Hi futurelet. This code has been working great until now. Would please assist me in case I need almost a reverse to accomplish. Now, the report should display all values in Megabytes(MB) only, regardless the size computed in bytes, KB, MB, GB or TB.
I appreciate your help.
Thanks.
 
Simply use totalsize/(1024*1024)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top