Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to reset my data using awk. I have a data of 54 columns where I want to change six of the column values using the following script.

#!/bin/bash -x
# optimal parameters
set BI2=0.02841513
set DS2=0.37837487
set WS2=0.85392380
set D22=1.00048351
set D32=0.63893676
set DSMAX2=28.15795517

# change the calibration parameter
awk '{if($1==2) {
  $6 = "${BI2}"
  $7 = "${DS2}"
  $8 = "${DSMAX2}"
  $9 = "${WS2}"
  $25 = "${D22}"
  $26 = "${D32}"}
  print $0;}' soil_params > tmp
mv -f tmp soil_params

my data looks like:

999 0   123 65.183534   -147.655522 0.2 0.001   10  0.9 2   10.84   9.79    9.76    812.42  1546.28 1334.35 -999    -999    -999    18.579  68.139  26.914  548 0.1 0.3 0.192752    -0.8725 4   35.39   30.13   19.34   0.323   0.347   0.382   1160.4  1196.2  1208.9  2186.08 2253.52 2411.44 -9.8    0.582   0.489   0.443   0.396   0.4841  0.28    0.001   0.0005  383.7   0.1 0.1 0.1 1   16.733299   16.733299                                           
2   1   124 65.183534   -147.639897 ${BI2}  ${DS2}  ${DSMAX2}   ${WS2}  2   10.84   9.79    9.77    812.51  1544.96 1331.78 -999    -999    -999    18.579  68.277  26.915  676 0.1 ${D22}  ${D32}  -0.529166   4   35.45   30.19   19.37   0.323   0.347   0.382   1160.3  1196.3  1208.8  2185.87 2253.63 2411.11 -9.8    0.582   0.49    0.443   0.396   0.4851  0.28    0.001   0.0005  400.8   0.1 0.1 0.1 1   16.32   16.32                                                       

when I run the above script, it didn't change the value from the parameter set assigned rather it simple changes to the parameters as you can

Would you please help me on this regards?

share|improve this question
add comment

2 Answers

the problem you are facing is that you are trying to use BASH-variables within the awk-command which is protected by single-quotes ('...'). within single quotes, bash will not expand the variables (e.g. ${BI2}), so awk will literally see $6 = "${BI2}" and it will set the value of the 6th column to the literal "${B12}".

luckily you can easily create awk variables from shell variables using the --assign (aka -v) argument:

awk \
   -v BI2="${BI2}" \
   -v DS2="${DS2}" \
   -v DSMAX2="${DSMAX2}" \
   -v WS2="${WS2}" \
   -v D22="${D22}" \
   -v D32="${D32}" \
  '{if($1==2)
     {
      $6 = BI2; $7 = DS2; $8 = DSMAX2; $9 = WS2; $25 =D22; $26 =D32; 
     }
    print $0;
  }'
share|improve this answer
add comment

You put the awk script in single quotes, this is usually recommended because it prevents the shell from trying to substitute variables defined by awk, like $1. Unfortunately, it also means that the shell can't substitute in the variables only it knows about, like $BI2. If you wrap your awk invocation in double quotes instead:

awk "{if(\$1==2) {
  \$6 = ${BI2}
  \$7 = ${DS2}
  \$8 = ${DSMAX2}
  \$9 = ${WS2}
  \$25 = ${D22}
  \$26 = ${D32}}
  print \$0;}" soil_params > tmp

then the shell will replace ${BI2} and such. However, now the variables that awk uses, like $1, have to be written as \$1 to prevent the shell from trying to provide values for them.

share|improve this answer
    
I got awk: ^ backslash not last character on line error. It doesn't execute the result as well. I want ${BI2} ${DS2} ${DSMAX2} ${WS2} .. etc to be replaced by thier values from the assigned set values. I put ${BI2} ${DS2} ${DSMAX2} ${WS2} in my sample data wrong. They are numeric values just the ame as the first row. –  Abraham Sep 30 '13 at 20:45
    
@Abraham When defining your variables, try replacing set with export, e.g. export BI2=0.02841513. –  wingedsubmariner Oct 1 '13 at 0:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.