Feb 21, 2004 #1 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
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
Feb 21, 2004 1 #2 CaKiwi Programmer Apr 8, 2001 1,294 US There's gotta be an easier way, but this seems to work. END { n = Tbytes j = 1000 while (j <= n) j *= 1000 fstr = "%d" while(j>999999) { j /= 1000 printf (fstr ",",int(n/j)) fstr = "%3.3d" n %= j } printf (fstr "\n", n) } CaKiwi Upvote 0 Downvote
There's gotta be an easier way, but this seems to work. END { n = Tbytes j = 1000 while (j <= n) j *= 1000 fstr = "%d" while(j>999999) { j /= 1000 printf (fstr ",",int(n/j)) fstr = "%3.3d" n %= j } printf (fstr "\n", n) } CaKiwi
Feb 22, 2004 1 #3 aigles Technical User Sep 20, 2001 464 FR Another solution : [tt] END { Tbytes += 0 ; res = ""; for (pos=length(Tbytes); pos>3; pos-=3) res = "," substr(Tbytes, pos-2,3) res; res = substr(Tbytes, 1, pos) res; print res; } [/tt] Jean Pierre. Upvote 0 Downvote
Another solution : [tt] END { Tbytes += 0 ; res = ""; for (pos=length(Tbytes); pos>3; pos-=3) res = "," substr(Tbytes, pos-2,3) res; res = substr(Tbytes, 1, pos) res; print res; } [/tt] Jean Pierre.
Feb 24, 2004 Thread starter #4 jwleger Programmer Nov 10, 2003 8 US 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' Upvote 0 Downvote
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'
Feb 24, 2004 #5 vgersh99 Programmer Jul 27, 2000 2,146 US you should probably consider polygamy as an option. vlad +----------------------------+ | #include<disclaimer.h> | +----------------------------+ Upvote 0 Downvote
you should probably consider polygamy as an option. vlad +----------------------------+ | #include<disclaimer.h> | +----------------------------+