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!

Awk print formatting 2

Status
Not open for further replies.

jwleger

Programmer
Nov 10, 2003
8
US
How do I output large numbers is the format NNN,NNN,NNN..., using awk print or printf. Please give me an example.

such as :

ls -l|awk '{Tbytes += $5} END {print "Total Bytes " Tbytes}'

Only output the total bytes in format NNN,NNN,NNN

Thanks,
Joe
 
There's gotta be an easier way, but this seems to work.

END {
n = Tbytes
j = 1000
while (j <= n) j *= 1000
fstr = &quot;%d&quot;
while(j>999999) {
j /= 1000
printf (fstr &quot;,&quot;,int(n/j))
fstr = &quot;%3.3d&quot;
n %= j
}
printf (fstr &quot;\n&quot;, n)
}

CaKiwi
 
Another solution :
[tt]
END {
Tbytes += 0 ;
res = &quot;&quot;;
for (pos=length(Tbytes); pos>3; pos-=3)
res = &quot;,&quot; substr(Tbytes, pos-2,3) res;
res = substr(Tbytes, 1, pos) res;
print res;
}
[/tt]

Jean Pierre.
 
Thanks to both of you but, since I'm not married to awk, I am using this sed script.

sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t a'
 
you should probably consider polygamy as an option.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top