macosbashawk

Awk multiplication gives zero


I am a bit new to using awk. My goal is to create a bash function of the form:

myfunction file column value

That takes the given column number in file, multiplies it by value and rewrites the file. For now I have written the following:

function multiply_column {
 file=$1
 column=$2
 value=$3
 awk -F" " '{print $col*mul}' col=$column mul=$value $file
}

My file looks like this:

   0.400000E+15    0.168933E+00   -0.180294E-44    0.168933E+00
   0.401000E+15    0.167689E+00   -0.181383E-44    0.167689E+00
   0.402000E+15    0.166502E+00   -0.182475E-44    0.166502E+00
   0.403000E+15    0.165371E+00   -0.183569E-44    0.165371E+00
   0.404000E+15    0.164298E+00   -0.184666E-44    0.164298E+00
   0.405000E+15    0.163284E+00   -0.185766E-44    0.163284E+00
   0.406000E+15    0.162328E+00   -0.186868E-44    0.162328E+00
   0.407000E+15    0.161431E+00   -0.187972E-44    0.161431E+00
   0.408000E+15    0.160593E+00   -0.189080E-44    0.160593E+00
   0.409000E+15    0.159816E+00   -0.190189E-44    0.159816E+00
   0.410000E+15    0.159099E+00   -0.191302E-44    0.159099E+00
   0.411000E+15    0.158442E+00   -0.192416E-44    0.158442E+00
   0.412000E+15    0.157847E+00   -0.193534E-44    0.157847E+00
   0.413000E+15    0.157312E+00   -0.194653E-44    0.157312E+00
   0.414000E+15    0.156840E+00   -0.195775E-44    0.156840E+00
   0.415000E+15    0.156429E+00   -0.196899E-44    0.156429E+00
   0.416000E+15    0.156081E+00   -0.198026E-44    0.156081E+00
   0.417000E+15    0.155796E+00   -0.199154E-44    0.155796E+00
   0.418000E+15    0.155573E+00   -0.200285E-44    0.155573E+00
   0.419000E+15    0.155413E+00   -0.201418E-44    0.155413E+00
   0.420000E+15    0.155318E+00   -0.202554E-44    0.155318E+00
   0.421000E+15    0.155285E+00   -0.203691E-44    0.155285E+00
   0.422000E+15    0.155318E+00   -0.204831E-44    0.155318E+00
   0.423000E+15    0.155414E+00   -0.205973E-44    0.155414E+00
   0.424000E+15    0.155575E+00   -0.207116E-44    0.155575E+00
   0.425000E+15    0.155802E+00   -0.208262E-44    0.155802E+00

I managed to just print the first column, but when I multiply it with my value, awk gives me 0. I tried my function with other files where data was formatted differently, and it worked perfectly. I also tried to combine it with bc, without any success.

Does anyone see why in this case awk gives 0 ?

Thanks in advance !

######### EDIT

I just found out that if my data file uses commas and not dots (i.e. 0,400000E+15 instead of 0.400000E+15), my function works fine. So somehow, somewhere, something is configured to understand commas as the scientific notation separator instead of dots. Does that ring a bell to anyone ?


Solution

  • Set LC_ALL=C before executing your script to get the most commonly expected behavior for this and other locale-dependent issues. See http://www.gnu.org/software/gawk/manual/gawk.html#Locales. Also don't pointlessly set FS to it's default value, do quote your shell variables (google that if you don't know why), and do fix the way you are setting your variables to use the form that produces the most intuitive results (see http://cfajohnson.com/shell/cus-faq-2.html#Q24):

    LC_ALL=C awk -v col="$column" -v mul="$value" '{print $col*mul}' "$file"
    

    Read the book Effective Awk programming, 4th Edition, by Arnold Robbins.