Special format for numeric print out


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Special format for numeric print out
# 1  
Old 02-24-2013
Special format for numeric print out

Hi,
I know this must be very simple and stupid question
but I could not find a proper answer anywhere else,

My script generates numeric values in the following format

3.01234567891E+03

I use %.11E in printf formatting, but I need to list my numbers as

0.30123456789E+04

basicly I need 0 as the first digit and then 11 digits after the decimal point.
# 2  
Old 02-24-2013
What are you using to print(i.e. shell script, perl, etc.)?
# 3  
Old 02-24-2013
Not sure if this is mathematically correct!
Code:
echo "3.01234567891E+03" | awk ' {
        d = $0;
        sub(/.*\+/, x, d);
        i = index($0, ".");
        d = sprintf ("%02d", d + ( i - 1 ));
        sub(/\./, x);
        $0 = "0."$0;
        sub(/\+.*/, "+"d);
} 1 '

# 4  
Old 02-24-2013
I am using shell script spacebar. My code is an AWK code

---------- Post updated at 09:53 PM ---------- Previous update was at 09:49 PM ----------

Bipi,
I don't think that this is much useful since my code generates hundred thousands of numeric results.
I need much more practical solution,
I use
printf ("%.11E", q)
I really need something similar.
# 5  
Old 02-25-2013
Quote:
Originally Posted by hayreter
I need much more practical solution,
I use
printf ("%.11E", q)
I really need something similar.
I wish there is a simple solution.

Are you aware of any mathematical solution that will adjust decimal places and power of without changing the value?
# 6  
Old 02-25-2013
Isn't it just a matter of formatting?
Assume the number is something like
123.456
I just need it to be written as
0.123456E+03
# 7  
Old 02-25-2013
Quote:
Originally Posted by hayreter
Isn't it just a matter of formatting?
Assume the number is something like
123.456
I just need it to be written as
0.123456E+03
Yes, it is just a matter of formatting. Unfortunately what you're asking for is not a format specified by the C, POSIX, or UNIX Standards as an fprintf() family of functions format specifier. So you need to create your own function to produce the format you want. Here is a function that seems to do what you want encapsulated in an awk script that will compare the standard "%.xE" formatting with the formatting produced by the function. The printf utility invocation before the awk script is used to produce a list of test values. The script:
Code:
printf "%d %s\n" 11 3.01234567891E+03 11 123.45678901 5 12345e200 \
5 1234e-200 15 1.23456789012345 4 -123456 11 -123456789012345 |
awk 'function printfEm(value, digits,  exponent, fmt, mantissa, result, sign, spot) {
        # Print a numeric <value> using the format string "%.<digits>E" and 
        # then reformat the result to have a leading 0 instead of a leading
        # non-zero digit.
        # Generate the sprintf() format string and format the given value.
        fmt = sprintf("%%.%dE", digits - 1)
        result = sprintf(fmt, value)
        # Save the sign of the result.
        if(substr(result, 1, 1) == "-") {
                sign = "-"
                # Discard the minus sign...
                result = substr(result, 2)
        } else  sign = ""
        # Find the exponent in the result.
        spot = index(result, "E")
        # Reformat the mantissa.
        mantissa = "0." substr(result, 1, 1) substr(result, 3, spot - 3)
        # Adjust the exponent.
        exponent = substr(result, spot + 1) + 1
        # Return the reformatted sign, mantissa, and exponent.
        return sprintf("%s%sE%+03d", sign, mantissa, exponent)
}
{       printf("Processing \"%%.%dE\" \"%s\"\n", $1, $2)
        printf("%."$1"E\n%s\n", $2, printfEm($2, $1))
}'

produces the output:
Code:
Processing "%.11E" "3.01234567891E+03"
3.01234567891E+03
0.30123456789E+04
Processing "%.11E" "123.45678901"
1.23456789010E+02
0.12345678901E+03
Processing "%.5E" "12345e200"
1.23450E+204
0.12345E+205
Processing "%.5E" "1234e-200"
1.23400E-197
0.12340E-196
Processing "%.15E" "1.23456789012345"
1.234567890123450E+00
0.123456789012345E+01
Processing "%.4E" "-123456"
-1.2346E+05
-0.1235E+06
Processing "%.11E" "-123456789012345"
-1.23456789012E+14
-0.12345678901E+15

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to add a numeric & special char to end of the first line

Need to add a numeric & special char to end of the first line Existing file: 12-11-16|11 2016 Jan 12:34:55|03:55| 13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55| 14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55| 15-10-16|18 2016 Jan... (11 Replies)
Discussion started by: Joselouis
11 Replies

2. UNIX and Linux Applications

Lpr send to print a4 format and print letter format

Hi! How we are? I have an A4 PDF in my server, and i must send it to phisically printer. I use the comand: lpr -P printername -o media=A4 archive.pdf And the printer prints it in letter format, i don't know why. ¿Have ideas or solution? Thanks, my best regards. (6 Replies)
Discussion started by: dcastellini
6 Replies

3. Shell Programming and Scripting

Print every 5 lines with special condition

Hi Friends, I have an input file like this chr1 100 200 chr1 200 300 chr1 300 400 chr1 400 500 chr1 500 600 chr1 600 700 chr1 700 800 chr1 800 900 chr1 900 920 chr1 940 960 I would like to get the first line's second column and the fifth line's 3rd column as one single line. This... (13 Replies)
Discussion started by: jacobs.smith
13 Replies

4. Shell Programming and Scripting

a cut-command or special format pattern in awk

Hi i read data with awk, 01.07.2012 00:10 227.72 247.50 1.227 1.727 17.273 01.07.2012 00:20 237.12 221.19 2.108 2.548 17.367 01.07.2012 00:30 230.38 230.34 3.216 3.755 17.412 01.07.2012 00:40 243.18 242.91 4.662 5.172 17.328 01.07.2012 00:50 245.58 245.41 5.179 5.721 17.128... (3 Replies)
Discussion started by: IMPe
3 Replies

5. Shell Programming and Scripting

print all between patterns with special chars

Hi, I'm having trouble with awk print all characters between 2 patterns. I tried more then one solution found on this forum but with no success. Probably my mistakes are due to the special characters "" and "]"in the search patterns. Well, have a log file like this: logfile.txt ... (3 Replies)
Discussion started by: ginolatino
3 Replies

6. UNIX for Dummies Questions & Answers

Only print lines with 3 numeric values

Hey guys & gals, I am hoping for some advice on a sed or awk command that will allow to only print lines from a file that contain 3 numeric values. From previous searches here I saw that ygemici used the sed command to remove lines containing more than 3 numeric values ; however how... (3 Replies)
Discussion started by: TAPE
3 Replies

7. UNIX for Dummies Questions & Answers

I need a special print

I have this: \2009_may\05-04-2009\05-04-2009(74) \2009_may\05-04-2009\05-04-2009(74)\05-04-2009(74)_0-999 \2009_may\05-04-2009\05-04-2009(74)_left \2009_may\05-04-2009\05-04-2009(74)_left\05-04-2009(74) \2009_may\05-04-2009\05-04-2009(74)_right... (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

8. Shell Programming and Scripting

Awk , Sed Print last 4 numeric characters

Hello All, I have been searching and trying this for a bit now. Can use some assistance. Large 5000 line flat file. bash, rhel5 Input File Sinppet: Fri Oct 30 09:24:02 EDT 2009 -- 1030 Fri Oct 30 09:26:01 EDT 2009 -- 73 Fri Oct 30 09:28:01 EDT 2009 -- 1220 Fri Oct 30 09:30:01 EDT... (9 Replies)
Discussion started by: abacus
9 Replies

9. Shell Programming and Scripting

script for month conversion in numeric format

Hi Experts, How to convert months into numeric format with the help of some script: Suppose I want: " Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sept | Oct | Nov | Dec " to be converted as : " 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 " Thanks in... (2 Replies)
Discussion started by: rveri
2 Replies

10. Shell Programming and Scripting

gawk print some special lines

Hi every body, i have this file example : TD1 TD2 TD3 . . .TDn <DIE_1> xxxxxx <\DIE_1> <TD1> information 1 inormation n <\TD1> <TDq> information (0 Replies)
Discussion started by: kamel.kimo
0 Replies
Login or Register to Ask a Question