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

Add Commas to Number Output 1

Status
Not open for further replies.

jtanner

Technical User
Joined
Feb 18, 2007
Messages
39
Location
US
Hello,

First off, thanks to everyone here for being so helpful in me getting up-to-speed in the strange new UNIX world! :-)

In a Bourne shell script I have a variable that contains a number (ex: 1049312). This number is in bytes and I'd like to make it more readable for my output. Ex. 1,049,312

How can I change a string that contains a number so as it has commas to make it easier to read?

Thanks,

JT
 
Hi

Or abit spectacular way : ;-)
Code:
command | sed 's/\([^,]\)\([^,]\{3\}\)$/\1,\2/;:r;s/\([^,]\)\([^,]\{3\},\)/\1,\2/;tr'
Tested with GNU [tt]sed[/tt].

Feherke.
 
Code:
# add commas to numeric strings, changing "1234567" to "1,234,567"
 sed ':a;s/\B[0-9]\{3\}\>/,&/;ta'                      # GNU sed
 sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'  # other seds



Jean-Pierre.
 


\B
Matches everywhere but on a word boundary; that is it matches if
the character to the left and the character to the right are
either both "word" characters or both "non-word" characters.

\>
Constrains a RE to match the end of a string or to
precede a character that is not a digit, underscore,
or letter.


All these escapes here are GNU extensions

Jean-Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top