Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Awk: Wondering how to format numbers with comma Post 302892069 by Scrutinizer on Monday 10th of March 2014 05:02:37 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
NUMFMT_GET_TEXT_ATTRIBUTE(3)						 1					      NUMFMT_GET_TEXT_ATTRIBUTE(3)

NumberFormatter::getTextAttribute - Get a text attribute

	Object oriented style

SYNOPSIS
public string NumberFormatter::getTextAttribute (int $attr) DESCRIPTION
Procedural style string numfmt_get_text_attribute (NumberFormatter $fmt, int $attr) Get a text attribute associated with the formatter. An example of a text attribute is the suffix for positive numbers. If the formatter does not understand the attribute, U_UNSUPPORTED_ERROR error is produced. Rule-based formatters only understand NumberFormat- ter::DEFAULT_RULESET and NumberFormatter::PUBLIC_RULESETS. PARAMETERS
o $fmt -NumberFormatter object. o $attr - Attribute specifier - one of the text attribute constants. RETURN VALUES
Return attribute value on success, or FALSE on error. EXAMPLES
Example #1 numfmt_get_text_attribute(3) example <?php $fmt = numfmt_create( 'de_DE', NumberFormatter::DECIMAL ); echo "Prefix: ".numfmt_get_text_attribute($fmt, NumberFormatter::NEGATIVE_PREFIX)." "; echo numfmt_format($fmt, -1234567.891234567890000)." "; numfmt_set_text_attribute($fmt, NumberFormatter::NEGATIVE_PREFIX, "MINUS"); echo "Prefix: ".numfmt_get_text_attribute($fmt, NumberFormatter::NEGATIVE_PREFIX)." "; echo numfmt_format($fmt, -1234567.891234567890000)." "; ?> Example #2 OO example <?php $fmt = new NumberFormatter( 'de_DE', NumberFormatter::DECIMAL ); echo "Prefix: ".$fmt->getTextAttribute(NumberFormatter::NEGATIVE_PREFIX)." "; echo $fmt->format(-1234567.891234567890000)." "; $fmt->setTextAttribute(NumberFormatter::NEGATIVE_PREFIX, "MINUS"); echo "Prefix: ".$fmt->getTextAttribute(NumberFormatter::NEGATIVE_PREFIX)." "; echo $fmt->format(-1234567.891234567890000)." "; ?> The above example will output: Prefix: - -1.234.567,891 Prefix: MINUS MINUS1.234.567,891 SEE ALSO
numfmt_get_error_code(3), numfmt_get_attribute(3), numfmt_set_text_attribute(3). PHP Documentation Group NUMFMT_GET_TEXT_ATTRIBUTE(3)
All times are GMT -4. The time now is 08:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy