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.
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:
The bash script follows:
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)
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)
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)
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)
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)
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)
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)
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)
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)