escaping in awk and bash script -


i'm trying change numbers in column 2, 3, , 4 of files. that, have following script

for in 0.8 0.9 1 1.1 1.2; original=10.9398135077 fraction=`bc <<< "$i*$original"` convert=0.529177 awk '{printf "%-2s %10.5f %10.5f %10.5f\n", $1, ($2*"\$fraction"*"\$convert"), ($3*"\$fraction"*"\$convert"), ($4*"\$fraction"*"\$convert")}' temp2_${i}.txt > coord_${i}.txt done 

and temp2_0.8.txt looks following:

cu      -0.000000000   0.000000000  -0.000000000 cu       0.500000000  -0.000000000   0.500000000 cu       0.000000000   0.500000000   0.500000000 cu       0.500000000   0.500000000   0.000000000 s        0.398420013   0.398420013   0.398420013 s        0.898420013   0.398420013   0.101579987 s        0.398420013   0.101579987   0.898420013 s        0.101579987   0.898420013   0.398420013 s        0.601579987   0.601579987   0.601579987 s        0.898420013   0.101579987   0.601579987 s        0.101579987   0.601579987   0.898420013 s        0.601579987   0.898420013   0.101579987 

but if execute script, message:

awk: warning: escape sequence `\$' treated plain `$' 

and 0.00000 in of converted files. seems failed escape keywords in awk.... how can multiply "fraction" , "convert" column number 2, 3, , 4 awk properly?

add:) used \$fraction instead of "\$fraction" gives me error "backslash not last character on line"

inside single quotes, shell ignores dollars, backslashes, double quotes, back-ticks — single quotes interesting. awk script therefore gets values wrong. use -v variable="$value" pass shell variables script. why bother bc? awk can multiply quite happily.

original=10.9398135077 convert=0.529177 in 0.8 0.9 1 1.1 1.2     awk -v org="$original" -v factor="$i" -v conv="$convert" \         'begin { multiplier = factor * original * convert }          { printf "%-2s %10.5f %10.5f %10.5f\n", $1,                   ($2 * multiplier),                   ($3 * multiplier),                   ($4 * multiplier) }' temp2_${i}.txt > coord_${i}.txt done 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -