The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
cut - columns with formatted Output dhanamurthy Shell Programming and Scripting 9 05-19-2008 10:20 AM
Formatted Output dhanamurthy Shell Programming and Scripting 6 05-13-2008 02:30 AM
Formatted output - awk dhanamurthy Shell Programming and Scripting 3 05-11-2008 11:25 PM
Formatted output in KSH psynaps3 Shell Programming and Scripting 1 07-05-2006 08:03 AM
count commas in a script obedkhan HP-UX 2 02-25-2006 10:35 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-29-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question formatted output with commas

var=12345
echo $var >>>12345

printf "%8.1f \n" $var >>> 12345.0

How to get this as 12,345?
I suppose I could break into sections by dividing by 1000 or 1000000.
But, is the a trick to this?
  #2 (permalink)  
Old 02-29-2008
Cranie Cranie is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 16
in this instance you could use:

Code:
 printf "%8.1f \n" $var | sed 's/...\./,&/g'
But for something with more than 5 characters or less than 4 this wouldn't work.
  #3 (permalink)  
Old 02-29-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Unhappy the number of digits is not fixed

The data to report is a counter; my guess is normally between one and seven digits. Thus I could want to see
Code:
1,234,567
  123,456
   12,345
    1,234
      123
       12
        1
I could be reporting in any of the above layouts - comma at every xxx, right-justified. I fear that I may be forced to break my variable counter into sections and handle three characters at a time.
  #4 (permalink)  
Old 02-29-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
sed to add commas in to numbers:

Code:
 echo 1234567 | sed -e :a -e 's/\([0-9][0-9]*\)\([0-9][0-9][0-9]\)/\1,\2/;ta'
HTH
  #5 (permalink)  
Old 03-04-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Thanks, it works

Someday, I will learn what all of those codes with the 'sed' mean
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0