awk:How to format a number?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk:How to format a number?
# 8  
Old 03-19-2010
Quote:
Originally Posted by ungalnanban
Good solution.
# 9  
Old 06-03-2010
Greetings from the void,

I am sed-centric, so here is a sed solution provided in an example form. I may ramble on a bit, but better than to starve for details. Smilie

The script creates a sequence of numbers from 1 to 15 digits, which are printed to the screen after they are formatted with commas.

The sed statement has an indirect logic. I'll try to explain.

The first pair of slashes test for a string having the minimum of characters required to place a comma (section1). The indirect part is that only select characters on the end of the string are tested, and there can be more characters preceding the tested portion, but is not required for the test.

The (2nd section) begins with s/ and has 2 match tests contained within the brackets,
like \( match_exp1 \) and the next \( match_exp2 \). Essentially this just splits the string so that the comma is placed correctly.
The next slash divides this section from the substitution section.

The (3rd section) reassembles the matched text, which is marked by the labels \1 and \2 which correspond to the matched text from the brackets in the preceding section 2. In between the labels is the comma needed for the formatting.

Each successive sed test/substitute statement, looks for commas added by the preceding sed substitute section. The code has a continue on (failed sed match) approach, where the number string gets passed thru if no matches are found. Essentially it will not add unneeded commas, but will retain any that were added in prior sed substitution sections.

Note that the semicolons in the sed statement are like seperate sed statements, but they are combined into a single pass, instead of using seperate sed invocations. I like to break my complex seds down to seperate lines because its easier to visualize/debug, and it runs just fine in this format.

Note that this code only works on integers, but it indubitably could be tweaked to handle decimal values. I would just parse the decimal and right of decimal into a variable, run this code on the remainder, which is then an integer, then reassemble the two parts for a comma formatted decimal number.

From the code below, with sections numbered:
Code:
 sed '/....$/ s/^\(.*\)\(...\)$/\1,\2/;
      1-----   2-------------- 3-----

The bash script follows:
Code:
#!/bin/sh
#
# testTube1.sh
#
# ### Notes ###
#
# The parser is limited to a number no greater than 1 x 10^15 digits.
# This is greater than the Tera range; in the Peta range.
# The range can easily be expanded - the sed pattern below is easy to mod.
# Power / 3 = number of commas needed. This script provides for 5 commas.
#
# m Mega 1 x 10^6
# g Giga 1 x 10^9
# t Tera 1 x 10^12
# p Peta 1 x 10^15
#
# 1 x 10^15 =     1,000,000,000,000,000
# 1 x 10^18 = 1,000,000,000,000,000,000
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# void before use
rawnum=

# make a sequence of numbers that increase exponentially in base 10
# aka { 5, 55, 555, 5555, etc... }
#
for i in `seq 1 18`
do

  # grow a number exponentially
  rawnum="${rawnum}5"

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# run the number thru the comma mill
#
echo $rawnum \
| sed '/....$/ s/^\(.*\)\(...\)$/\1,\2/;
/....,...$/ s/^\(.*\)\(...,...\)$/\1,\2/;
/....,...,...$/ s/^\(.*\)\(...,...,...\)$/\1,\2/;
/....,...,...,...$/ s/^\(.*\)\(...,...,...,...\)$/\1,\2/;
/....,...,...,...,...$/ s/^\(.*\)\(...,...,...,...,...\)$/\1,\2/'
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

done
# # #
# EOF
#

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