Sponsored Content
Top Forums Shell Programming and Scripting Special format for numeric print out Post 302772319 by Don Cragun on Monday 25th of February 2013 04:59:56 AM
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:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 11:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy