awk:How to format a number?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk:How to format a number?
# 1  
Old 03-19-2010
awk:How to format a number?

Hello,

I need to format a number..like 12900 should be printed as 12,900
and 1209 as 1,209 and so on. (Just like we do in excel).

Can this be done in awk. any printf options we have?Please suggest me.

Thanks!
# 2  
Old 03-19-2010
# 3  
Old 03-19-2010
Refer the following link

The AWK Manual - Printf Examples
# 4  
Old 03-19-2010
MySQL

Quote:
Originally Posted by murugaperumal
Refer the following link

The AWK Manual - Printf Examples


murugaperumal, I referred the above link, The information is not related to formating the number in human readable format.
# 5  
Old 03-19-2010
Not an option to printf but, I found this:


Code:
 
echo 1234 | nawk ' { printf("This should have commas: %d,%d\n",$0/1000,$0%1000); } '

This should have commas: 1,234
# 6  
Old 03-19-2010
MySQL

Quote:
Originally Posted by pmm
Not an option to printf but, I found this:


Code:
 
echo 1234 | nawk ' { printf("This should have commas: %d,%d\n",$0/1000,$0%1000); } '

This should have commas: 1,234

pmm, your code is adding the comma from last three digits before only.

Example:

1234567 => 1234,567 it should be 1,234,567.
# 7  
Old 03-19-2010
yes, But I'm afraid to say that for numbers greater than 10000 this logic may not work out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk command to convert number occurances into date format and club a set of lines

Hi, I have been stuck in this requirement where my file contains the below format. 20150812170500846959990854-25383-8.0.0 "ABC Report" hp96880 "4952" 20150812170501846959990854-25383-8.0.0 End of run 20150812060132846959990854-20495-8.0.0 "XYZ Report" vg76452 "1006962188"... (6 Replies)
Discussion started by: Chinmaya Kabi
6 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

4. Shell Programming and Scripting

how to format a number in bash

Say I have a number x=123, but I want to it be x=000123, because I need to use it in as a file name. thanks! (2 Replies)
Discussion started by: aerosols
2 Replies

5. Shell Programming and Scripting

Float number format problem -Awk

Here is the script I'm using awk '{print $1,"\t",(($2+$3)/2)-x,"\t",(($2+$3)/2)+x,"\t",$4,"\t",$5}' x=500 $1 I just want to make float numbers (red) like normal numbers (green) output cX 1.65107e+08 1.65108e+08 13 64.2 cX 165112764 165113764 27 ... (7 Replies)
Discussion started by: ruby_sgp
7 Replies

6. Shell Programming and Scripting

Need to change format of number

Hi, using a shell script to get values from a CSV eg: 12345.67,5678990.89,76232882.90 12345,5678990.89,76232882 Need the format of these numbers to change to 12,345.67:5,678,990.89:76,232,882.90 12,345:5678990.89:76232882 Using nawk on solaris, to parse these values, need the... (10 Replies)
Discussion started by: pgop
10 Replies

7. Web Development

Need Number Format Help in PHP

I have a number coming into a php echo statement that looks like 0293 and i want to output it looking like 29.3 and for the life of me i cannot figure out how to do it with available php functions like number_format, sprintf, or printf. Sample Data: 0293 0304 0282 0310 1324 2000 ... (2 Replies)
Discussion started by: RacerX
2 Replies

8. Shell Programming and Scripting

number format in Perl

I try to read in a file and write out a new file with increased number at the end of each line. And I can set the initial value and increased constant from inputs. input file: text1 text2 text3 ... text100 if I set initial value is 10, and increased constant is 0.4 output file: text1... (3 Replies)
Discussion started by: jinsh
3 Replies

9. Shell Programming and Scripting

Help with format a number in a file

Hey, I have a file which starts each line with 6 digits followed bya colon: 090607:The rest of the line 091207:Also some text 091207:Here's some more text And I want to reformat them into: 06-09-07:The rest of the line 12-09-07:Also some text 12-09-07:Here's some more text I... (3 Replies)
Discussion started by: kabatsie
3 Replies

10. Shell Programming and Scripting

How to format number/string in ksh

Hi, How to format a number in ksh. For example x=RANDOM $$ I want x to be of 20 digits long, so if x = 12345 I want it to be left paded with 15 zeros. Thanks. (2 Replies)
Discussion started by: GNMIKE
2 Replies
Login or Register to Ask a Question