Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Fortran Format 2 1

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I had previously submitted the same question and PHV did answer, however, the short script of PHV seems not to work.

I have tried with this script, but still not works.
{

for(i=1; i <= NF; i++)

printf ("%10.4e",$i);
}


I am trying to format a large file.

An example input file is below:


0 0 0 0 0.00120833 0.00331665 0.00335832 0.00339165 0.00342499 0.00295832
0 0 0 0 0 0 0.000816663 0.00241666 0.00224999 0.00227499
0.000841663 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0


The output file would be like:

0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 1.2000e-03 3.3000e-03 3.4000e-03 3.4000e-03 3.4000e-03 3.0000e-03
0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 8.0000e-04 2.4000e-03 2.2000e-03 2.3000e-03
8.0000e-04 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00
0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00

The output file has a fortran format as: 10e12.4


Could someone please help as how to make this format in awk. I am new with awk.

Thanks.
 
I don't think you understood that PHV was giving you a hint, rather than a script.
Code:
{
   for (x=1; x<=NF; x++)
      $x = sprintf("%.4e", $x)
   print $0
}

In future please do not start a new thread for the same question.

 
Thanks Ygor, it works, the results below. However, I would have liked it in the format of 10e12.4, what this means is that each field will have total of 10 characters in the string, of which 2 extra character makes up two blank spaces between the next field. In short it should be like
1.2346e-02 2.4532e-03 4.5642e-01

Your results gives 11 characters, with one blank space between two fields:
1.2346e-002 2.4532e-003 4.5642e-001



0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 1.2083e-003 3.3167e-003 3.3583e-003 3.3917e-003 3.4250e-003 2.9583e-003
0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 8.1666e-004 2.4167e-003 2.2500e-003 2.2750e-003
8.4166e-004 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000
0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000 0.0000e+000
 
If you want two spaces between your fields set OFS, e.g...
Code:
BEGIN {OFS = "  "}
{
   for (x=1; x<=NF; x++)
      $x = sprintf("%.4e", $x)
   print $0
}
 
Thanks Ygor. It works and gave me two extra space. However, I need to get two digit exponential value not three digit as your output result gives.

example, the correct format is: 1.2345e-02

where as yours is: 1.2345e-002
 
I cannot control how many digits printf uses to represent the exponent on your system. On my system, printf uses a minimum of two digits, like this...
[tt]
1.2346e-02 2.4532e-03 4.5642e-01 0.0000e+00
[/tt]
Since your system uses a minimum of three digits then you will either have to consult the manual for printf or use substr to extract the portion that you want.
Code:
{
   for (x=1; x<=NF; x++) {
     $x = sprintf("%.4e", $x)
     $x = substr($x, 1, 8) substr($x, length($x)-1, 2)
   }
   print $0
}
 
Thanks a lot. Truly appreciate your kind heart.
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top