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!

DECIMAL PLACES WITH AWK

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have the following awk command which returns multiple decimal places:

nawk '$5 > 90 {$2=$2/1024;$3=$3/1024;$4=$4/1024;printf("%-20s %-9s %-9s %-9s %-10s %-10s\n",$1,$2,$3,$4,$5,$6)}' file

How do I go about making the output display 2 decimal places? No decimal places?

Any help would be greatly appreciated.

Thanks,

John




 
Like....

printf("%-20s %9.2f %9.1f %9.0f %-10s %-10s\n",$1,$2,$3,$4,$5,$6)}'

man printf for more details
 
Thanks, Ygor. I also want to test to see if the 5th field contains a 100. Can I do two tests within the same awk statement? I tried doing the following (which doesn't work):

nawk '$5 > 90 || $5 = 100{$2=$2/1024;$3=$3/1024;$4=$4/1024;printf("%-20s %-9s %-9s %-9s %-10s %-10s\n",$1,$2,$3,$4,$5,$6)}' file

Any help would be greatly appreciated.

Thanks,

John
 
$5 == 100

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks, Vlad. That worked, but since the field had a % sign as well I had to add that and backslash escape it.

nawk '$5 > 90 || $5 == &quot;100\%&quot; {$2=$2/1024;$3=$3/1024;$4=$4/1024;printf(&quot;%-20s %-9s %-9s %-9s %-10s %-10s\n&quot;,$1,$2,$3,$4,$5,$6)}' file

Thanks,

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top