Data stream between min and max

Hi,
I have a text file containing numbers. There are up to 6 numbers per row and I need to read them, check if they are 0 and if they are not zero check if they are within a given interval (min,max). If they exceed the max or min they should be set to max or min respectively, if they are in the interval they retain their values.
I have done this with Excel but it is quite dull. Is there a way that you can think of without writing a C program?
This is an example of the input

  0.36601E-02  0.44246E-02  0.30417E-02  0.41934E-02  0.43863E-02  0.35131E-02
  0.37925E-02  0.50714E-02  0.39849E-02  0.31404E-02  0.46017E-02  0.41518E-02
  0.62381E-02  0.90390E-03  0.31362E-03  0.74747E-01   0.0000       0.0000    
   0.0000       0.0000      0.44737E-01  0.37985E-03  0.46412E-02  0.25617    
  0.35068      12333.8      0.36850      0.36416      0.31733      0.32028    

output (with min=0.001 max=1000)

  0.36601E-02  0.44246E-02  0.30417E-02  0.41934E-02  0.43863E-02  0.35131E-02
  0.37925E-02  0.50714E-02  0.39849E-02  0.31404E-02  0.46017E-02  0.41518E-02
  0.62381E-02  1.00000E-03  1.0000E-03  0.74747E-01   0.0000       0.0000    
   0.0000       0.0000      0.44737E-01  0.37985E-03  0.46412E-02  0.25617    
  0.35068      1.00000E+03  0.36850      0.36416      0.31733      0.32028    

Thanks,
Sarah

awk -v min=0.001 -v max=1000.0 '{
  $1 = $1 # the same formating for all lines 
  for (i=1; i<=NR; i++) 
    if ($i)                 
      if ($i < min) $i = min     
      else if ($i > max) $i = max
  print     
}' INPUTFILE

You can change OFS and OFMT variables to format output of values.

1 Like

Thank you,
I tried to set OFMT

awk -v min=0.001 -v max=1000.0 '{
  OFMT="%12.5e"
  $1 = $1 # the same formating for all lines 
  for (i=1; i<=NR; i++) 
    if ($i)                 
      if ($i < min) $i = min     
      else if ($i > max) $i = max
  print     
}' INPUTFILE

but it doesn't seem to work.

Oops. I made an awful typo - not NR, but NF. Well, I'm seeing about OFMT.

===

Yes, OFMT is useless (it's just for "print FLOAT"). You can do this:

awk -v min=0.001 -v max=1000.0 '
{
  for (i=1; i<=NF; i++)
    if ($i)
      if ($i < min) $i = min
      else if ($i > max) $i = max
  for (i=1; i<=NF; i++)
    printf "%12.5e", $i
  print ""
}' INPUTFILE
 3.66010e-03 4.42460e-03 3.04170e-03 4.19340e-03 4.38630e-03 3.66010e-03
 3.79250e-03 5.07140e-03 3.98490e-03 3.14040e-03 4.60170e-03 5.07140e-03
 6.23810e-03 1.00000e-03 1.00000e-03 7.47470e-02 0.00000e+00 1.00000e-03
 0.00000e+00 0.00000e+00 4.47370e-02 1.00000e-03 4.64120e-03 1.00000e-03
 3.50680e-01 1.00000e+03 3.68500e-01 3.64160e-01 3.17330e-01 3.17330e-01
1 Like

It's works thank you!