Awk: Wondering how to format numbers with comma


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk: Wondering how to format numbers with comma
# 1  
Old 03-10-2014
Awk: Wondering how to format numbers with comma

I have tried the following commands and can't get a number to format with commas:

Code:
echo 1234567.12 |awk '{printf("%-12s %20s\n", $0, comma($0)) }'


This prints out value 50000 without a comma

Code:
for i in *13[0-1]*; do (cd $i && du -sk . && echo $i);done|grep -v 0000|gawk -F OFS="," ' {SUM += $1} END {  print "Total KB" "\t" SUM }'

Code:
echo 10000000 | gawk '{ printf ("%'"'"'d\n", $1) }'

for the first command, I am not sure if I can use awk -F OFS to print a comma, but there are no delimiters such as a colon in the output, so probably not. The third command I found, but it does not print a comma. Does anyone know how to print a comma with awk?
# 2  
Old 03-10-2014
Try something like:
Code:
echo 1234567.12 | LC_NUMERIC=nl_NL.UTF-8 awk '{printf("%7.2f\n", $0) }

--edit--
OK, the above only works with BSD awk, unfortunately and not even that, since the decimals get reset to 00.


---
These would be work-arounds:
Code:
echo 1234567.12 | awk '{f=sprintf("%7.2f", $0); sub(/\./,",",f); print f }'

Code:
echo 1234567.12 | awk '{split($1,F,/\./); print F[1] "," F[2]}'

Code:
echo 1234567.12 | awk '{$1=$1}1' FS=. OFS=,

Code:
echo 1234567.12 |awk '{sub(/\./,",",$1)}1'

On Solaris use /usr/xpg4/bin/awk

Last edited by Scrutinizer; 03-10-2014 at 07:22 PM..
# 3  
Old 03-10-2014
doesn't work as I don't have the BSD. I am on Solaris 10. Anyone else used a comma in awk format?
# 4  
Old 03-10-2014
You have to tell gawk to use the locale LC_NUMERIC setting, e.g.
Code:
echo 1234567.12 | LC_NUMERIC=nl_NL.UTF-8 gawk -N '{printf("%7.2f\n", $0) }'
1234567,00

This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 03-10-2014
Quote:
Originally Posted by fpmurphy
You have to tell gawk to use the locale LC_NUMERIC setting, e.g.
Code:
echo 1234567.12 | LC_NUMERIC=nl_NL.UTF-8 gawk -N '{printf("%7.2f\n", $0) }'
1234567,00

There's no gawk on Solaris (by default)...
EDIT: oh, but the OP seems to have gawk installed based on the original post - sorry...
# 6  
Old 03-10-2014
Hi.

Long discussion and several solutions including some non-GNU at https://groups.google.com/forum/#!to...wk/J06ZD3ZSbys -- comp.lang.awk is a good place to hang out to see lots of ideas ... cheers, drl
This User Gave Thanks to drl For This Post:
# 7  
Old 03-10-2014
Thanks!

Thanks all for your helpful ideas!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum up formatted numbers with comma separation

I need to sum up the values in field nr 5 in a data file that contains some file listing. The 5th field denotes the size of each file and following are some sample values. 1,775,947,633 4,738 7,300 16,610 15,279 0 0 I tried the following code in a shell script. awk '{sum+=$5} END{print... (4 Replies)
Discussion started by: krishmaths
4 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Convert listner.log to csv format with comma seperated

Hi All, I am new to shell scripting i am trying to convert the listner.log to csv which can be inturn converted to excel for easy reading. i used this command awk '/SID=/ && /HOST=/ && /PORT=/ && /USER=/ { i=match($0,"SID="); i=i+RLENGTH; h0=substr($0,i); i=match(h0,")");... (6 Replies)
Discussion started by: skoshekay
6 Replies

4. Shell Programming and Scripting

awk to format file and combine two fields using comma

I am trying to use awk to format the file below, which is tab-delimited. The desired out is space delimited and is in the order of $9 $13 $2 $10-$11.$10 and $11 are often times multiple values separated by a comma, so the value in $10 is combined with the first value from $11 using the comma.... (5 Replies)
Discussion started by: cmccabe
5 Replies

5. UNIX for Dummies Questions & Answers

appending a command to print a file in a comma delimited format

Hi everyone, i have a file that I had grep'd from something else lets call it file1.txt which consists variable files and lines due to different scenarios/inputs 1782 9182 fe35 ac67 how can I print this in this manner? 1782,9182,fe35,ac67 also if i had piped the new output... (2 Replies)
Discussion started by: prodigy06
2 Replies

6. Shell Programming and Scripting

Removing comma just from numbers

Hi Guys, I want to remove commas from just the numbers in the file. So both sides of the comma should be numbers. Input file Johan 1,234 nb jan 123,3 hi, hello, bye 12,345,54 hejhej Desired output: Johan 1234 nb jan (6 Replies)
Discussion started by: Johanni
6 Replies

7. Shell Programming and Scripting

How to format file into comma separated field

Guys, Need you help, i have a a file content that look like this. Nokia 3330 <spaces><spaces><more spaces>+76451883874 Nokia 3610 +87467361615 so on and so forth, - there are so many spaces in between. - e.g.... (5 Replies)
Discussion started by: shtobias
5 Replies

8. UNIX for Advanced & Expert Users

Urgent! need help! how to convert this file into comma delimited format

Hi experts, I need urget help! I have the a text file with this format: Types of fruits Name of fruits 1,1 Farm_no,1 apple,1 pineapple,1 grapes,1 orange,1 banana,1 2,2--->this is the record seperator Farm_no,2 apple,1 pineapple,1 grapes,3 orange,2 banana,1 3,3--->this is the... (2 Replies)
Discussion started by: natalie23
2 Replies

9. Shell Programming and Scripting

how to convert this file into comma delimited format

Hi experts, I need urget help! I have the a text file with this format: Types of fruits Name of fruits 1,1 Farm_no,1 apple,1 pineapple,1 grapes,1 orange,1 banana,1 2,2--->this is the record seperator Farm_no,2 apple,1 pineapple,1 grapes,3 orange,2 banana,1 3,3--->this is the... (1 Reply)
Discussion started by: natalie23
1 Replies

10. UNIX for Dummies Questions & Answers

validate a pattern of numbers that are comma separated

Hi, I have a requirement wherein, I need to validate a user input of the numbers that are comma separated. E.g . The input should be in the format 1,2,3...n (count of numbers is not known) . The user has to mention the input in this format, else it should exit from the program. ... (5 Replies)
Discussion started by: 12345
5 Replies
Login or Register to Ask a Question