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

Number and Adding comma.

Status
Not open for further replies.

Dorga

MIS
Jul 30, 2003
101
NZ
I am generating data, and the final output is in the form of numbers ranging from one digit to 12, and I would just like to add comma's in from the right side to indicate thousands, etc.. just to make reding easier. (Output is Kbytes from another prog)

I want to convert 111111 to 111,111 or 11111 to 11,111 when I am printing the output.

Whats the magic?
 
Code:
echo '111111111111' | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'

sed1liners

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
As we are in the awk forum:
echo '1234567890123' | awk '{for(i=length($1)-2;i>1;i-=3)$1=substr($1,1,i-1)","substr($1,i);print}'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
That was sacrilegious of me - sorry 'bout that!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thsnks guys, im quite happy to know both solutions. =-)
 
Out of curiosity:

can you guys do this for decimal points too?


 
For decimal point, simply use sprintf.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
gawk --re-interval '
{ do { n=length($0)
       $0=gensub( /(.*[0-9])([0-9]{3})/, "\\1,\\2", "g")}
  while ( length($0) > n )
} 8' infile
[tt]Can you hear 1234567 trombones or 938268346127 warblers?
Or 1234?
123
[/tt]
is changed to
[tt]
Can you hear 1,234,567 trombones or 938,268,346,127 warblers?
Or 1,234?
123[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top