Displaying Number with printf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Displaying Number with printf
# 1  
Old 02-11-2008
Displaying Number with printf

I am trying to display a number with commas
Code:
printf "%d\n" 323232     
printf "%d\n" 1234567

I want the output to be:
Code:
323,232     
1,234,567

I tried to change %d to other formats and could find the solution.
any idea?
# 2  
Old 02-11-2008
Search the Web for a script called nicenumber.sh

Aternatively use this sed one liner which handles up to 3 decimal places correctly
Code:
$ echo 1234567 | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
1,234,567
$ echo 1234567.123 | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
1,234,567.123


Last edited by fpmurphy; 02-11-2008 at 06:42 AM..
# 3  
Old 02-11-2008
works good, thanks
Do yo have a solution within awk ?
# 4  
Old 02-11-2008
well, you can do it like this with sed:

Code:
#  echo 12345678 | sed -e :x -e 's/\([0-9][0-9]*\)\([0-9][0-9][0-9]\)/\1,\2/' -e 'tx'
12,345,678

# 5  
Old 02-11-2008
AWK solution

I found almost working solution
Code:
echo 1 12 123 1234 12345 123456 1234567 | awk --re-interval '{print gensub(/([[:digit:]])([[:digit:]]{3})/,"\\1,\\2","g")}' 

  1 12 123 1,234 1,2345 1,23456 1,234567

any idea how to fix it ?
# 6  
Old 02-11-2008
From the gawk manual

A single quote or apostrohe character is a POSIX extension to ISO C. It indicates that the integer part of a floating point value, or the entire part of an integer decimal value, should have a thousands-separator character in it. This only works in locales that support such characters.

Code:
     $ cat thousands.awk                                   
     BEGIN { printf "%'d\n", 1234567 }
     $ LC_ALL=C gawk -f thousands.awk                          
     1234567
     $ LC_ALL=en_US.UTF-8 gawk -f thousands.awk             
     1,234,567

# 7  
Old 02-12-2008
this is not working under linux
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Printf() not displaying as it should.

Hi, I have some code. Everything works as it should, but, when I call view_all_contacts() to print the data, each line doesn't line up as it should. I get tab keys between each line. The problem code is this: printf("\n\eHere is the rest of the code: void add_contact();... (5 Replies)
Discussion started by: ignatius
5 Replies

2. Shell Programming and Scripting

Adding user name to file, and then displaying new line number

Hi all - I'm completely stumped by a script I'm working on... The short version is I have a file called 'lookup' and in it are hundreds of names (first and last). I have a script that basically allows the user to enter a name, and what I need to have happen is something like this: Record... (8 Replies)
Discussion started by: sabster
8 Replies

3. Shell Programming and Scripting

Not able to sort two fields and printf not displaying the correct values

Not able to sorting two fileds resolved printf issue 01-1000/9|JAN 01-0000/6|MAN 01-1010/2|JAN 01-1010/2|JAN 01-1010/2|JAN 01-1000/9|JAN 01-1000/9|JAN 01-1000/9|SAA 01-1000/9|SAA 01-0000/6|SAN 01-0000/6|SAN 1.sort -t'|' -k1,1n -k2,2 file (3 Replies)
Discussion started by: kalia4u
3 Replies

4. Shell Programming and Scripting

Displaying a number in binary using perl

printf FH2" 3'b%b : begin\n",$i; where i is an integer in the loop is displaying 3'b1 : begin expected output was 3'b001 : begin (1 Reply)
Discussion started by: dll_fpga
1 Replies

5. Shell Programming and Scripting

Displaying lines of a file which have the highest number?

Hello Wondering if anybody may be able to advise on how I can filter the contents of the following file: <object_name>-<version> <Instance> GM_GUI_code.fmb-4 1 GM_GUI_code.fmb-5 1 GM_GUI_code.fmx-4 ... (7 Replies)
Discussion started by: Glyn_Mo
7 Replies

6. Shell Programming and Scripting

AWK: formating number without printf

Hello, I wrote a script that does lot of things, and I would like to change the format of a number but without printing it now (so I don't want to use printf as it will print the value immediately). Schematically here is what I have: awk 'BEGIN{number=0.01234567} $1==$2{$3=number}... (5 Replies)
Discussion started by: jolecanard
5 Replies

7. Shell Programming and Scripting

Displaying number of lines from file

Hi, I am using below command to display the number of line, but its returning no of lines along with file name. But i want only no of line in the variable p. Please help me on this? p=`wc -l "text file"` echo "$p" (6 Replies)
Discussion started by: shivanete
6 Replies

8. UNIX for Dummies Questions & Answers

Displaying Number with printf

I am trying to display a number with commas printf "%d\n" 323232 printf "%d\n" 1234567 I want the output to be: 323,232 1,234,567 I tried to change %d to other formats and could find the solution. any idea? (7 Replies)
Discussion started by: ynixon
7 Replies

9. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies

10. Shell Programming and Scripting

printf returning unknown number

Hi, Can anybody tell me why this function returns 49? printf "%d\n" \'10 I don't understand what the \ and ' are there for? thanks!!! (1 Reply)
Discussion started by: m0nk3y
1 Replies
Login or Register to Ask a Question