Special format for numeric print out


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Special format for numeric print out
# 8  
Old 02-25-2013
Another brew:
Code:
awk '{
  split(sprintf(fmt,$1),N,/E/)            # Convert input number to scientific format and split it into array N, using "E" as field separator
  ofmt=fmt                                # Set output format to input format 
  sub(/E/,"fE",ofmt)                      # Replace "E" by "fE" in the output format
  printf ofmt "%+03d\n",N[1]/10,N[2]+1    # Print the part before the E /10 and after E+1
}' fmt='%.11E'                            # Set f to the precision

These 4 Users Gave Thanks to Scrutinizer For This Post:
# 9  
Old 02-25-2013
Scrutinizer, this is a really nice code, but I think part after E should to be calculated according to how many decimal places we are moving. Isn't that correct?
Code:
awk '{
  i = index($0, ".");
  split(sprintf(fmt,$1),N,/E/)
  ofmt=fmt
  sub(/E/,"fE",ofmt)
  printf ofmt "%+03d\n",N[1]/10,N[2]+(i-1)
}' fmt='%.11E'

# 10  
Old 02-25-2013
Quote:
Originally Posted by Scrutinizer
Another brew:
Code:
awk '{
  split(sprintf(fmt,$1),N,/E/)            # Convert input number to scientific format and split it into array N, using "E" as field separator
  ofmt=fmt                                # Set output format to input format 
  sub(/E/,"fE",ofmt)                      # Replace "E" by "fE" in the output format
  printf ofmt "%+03d\n",N[1]/10,N[2]+1    # Print the part before the E /10 and after E+1
}' fmt='%.11E'                            # Set f to the precision

Hi Scrutinizer,
Thanks for this alternative. Note, however, that both of our proposals produce the wrong results for any zero value and for any infinity. And, our proposals will sometimes produce different value for the last digit of the mantissa due to rounding differences. (Yours will round the converted value divided by 10; mine will round the original value.) Our proposals will also produce different results for some subnormal values.

As examples:
Code:
Input_value     Scrutinizer_output      Don_Cragun_output
123456789555    0.12345678955E+12       0.12345678956E+12
0               0.00000000000E+01       0.00000000000E+01
1e400           infE+01                 0.IE-9223372036854775808
1e-312          1.00000000000E-312      0.10000000000E-311

If the differences between zeros and infinities are important, I'm sure the person with the original question can add checks for those conditions and produce the desired output manually. Subnormals are more difficult to detect and reformat.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 02-25-2013
@bipinajith: I don't think it is. The input ($1) is just any number that would be acceptable to awk. It then gets converted to scientific format through the sprintf statement..

@Don: That is really interesting. Good to know these corner cases (and zero Smilie ) Smilie

Last edited by Scrutinizer; 02-25-2013 at 12:13 PM..
# 12  
Old 02-25-2013
Thanks Don Cragun,
this is exactly what I was looking for.

---------- Post updated at 10:15 AM ---------- Previous update was at 10:10 AM ----------

And thanks Scrutinizer as well, luckily roundings and infinities do not matter for my case Smilie

---------- Post updated at 10:55 AM ---------- Previous update was at 10:15 AM ----------

Scrutinizer, could you please tell me how am i gonna embed your script into mine?
my awk code simply like this;

Code:
awk '{ does some search, replace, algebra and etc.
q1 = some algebraic calculation
q2 = some algebraic calculation
q3 = some algebraic calculation
q4 = some algebraic calculation

printf ("%s  %.11E  %.11E  %.11E  %.11E\n",block,q1,q2,q3,q4)
}' data

this basically prints a block (which has already several fields in it) as it is and those other
variables (q1,q2,q3,q4) in desired format
# 13  
Old 02-25-2013
Try putting it in function, for example:

Code:
awk '
  function pre( fmt, nr,	ofmt, N) {
    split(sprintf(fmt, nr),N,/E/)                  # Convert input number to scientific format and split it into array N, using "E" as field separator
    ofmt=fmt                                       # Set output format to input format 
    sub(/E/,"fE",ofmt)                             # Replace "E" by "fE" in the output format
    return sprintf(ofmt "%+03d",N[1]/10,N[2]+1)    # return the part before the E /10 and after E+1
  }
  { 
    q1=3.01234567891E+03
    q2=123.45678901
    q3=12345e200
    q4=1234e-200 
    printf "%s %s %s %s\n", pre( "%.11E", q1), pre( "%.11E", q2),pre( "%.11E", q3),pre( "%.11E", q4) 
  }
'

This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 02-26-2013
Hi.

An 8-line Fortran program into which you can pipe the data to do the formatting is illustrated here, along with a driver script:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate Fortran formatting.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C gfortran

FILE=${1-data1}

s=f1.f90
pl " Fortran source, $s:"
cat $s

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
rm -f a.out
gfortran $s
./a.out < data1

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
gfortran GNU Fortran (Debian 4.3.2-1.1) 4.3.2

-----
 Fortran source, f1.f90:
     program f1
! @(#) f1	Demonstrate Fortran-90 formatting.
       do
         read(*,*,end=100) x
         write(*,fmt="(e18.11)") x
       end do
100    continue
     end program f1

-----
 Input data file data1:
3.01234567891E+03
123.45678901
12345e200
1234e-200
-123456
1e400
1e-312
0

-----
 Results:
 0.30123457031E+04
 0.12345678711E+03
         +Infinity
 0.00000000000E+00
-0.12345600000E+06
         +Infinity
 0.00000000000E+00
 0.00000000000E+00

Best wishes ... cheers, drl
This User Gave Thanks to drl 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