简体   繁体   English

在awk和bash脚本中转义

[英]escaping in awk and bash script

I'm trying to change the numbers in column 2, 3, and 4 of the files. 我正在尝试更改文件的第2、3和4列中的数字。 To do that, I have following script 为此,我有以下脚本

for i in 0.8 0.9 1 1.1 1.2; do
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 my temp2_0.8.txt looks like following: 我的temp2_0.8.txt如下所示:

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 I execute my script, I get this message: 但是,如果我执行脚本,则会收到以下消息:

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

And I get 0.00000 in all of my converted files. 我所有转换后的文件中都得到0.00000。 It seems I failed to escape keywords in the awk.... how can I multiply "fraction" and "convert" to the column number 2, 3, and 4 with awk properly? 看来我无法在awk中转义关键字。...如何将awk正确地将“分数”和“转换”乘以第2、3和4列?

Add:) I used \\$fraction instead of "\\$fraction" but it gives me error "backslash not last character on line" 添加:)我使用\\ $ fraction而不是“ \\ $ fraction”,但是它给我错误“反斜杠不是行中的最后一个字符”

Inside single quotes, the shell ignores dollars, backslashes, double quotes, back-ticks — only single quotes are interesting. 在单引号内,shell会忽略美元,反斜杠,双引号,反引号-只有单引号很有趣。 The awk script therefore gets the values wrong. 因此, awk脚本获取的值有误。 Use -v variable="$value" to pass shell variables to the script. 使用-v variable="$value"将外壳变量传递给脚本。 Why bother with bc ? 为什么要打扰bc awk can multiply quite happily. awk可以很高兴地繁殖。

original=10.9398135077
convert=0.529177
for i in 0.8 0.9 1 1.1 1.2
do
    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

You don't need a shell loop for this: 您不需要为此的shell循环:

awk '
BEGIN {
    original = 10.9398135077
    convert = 0.529177
    split("0.8 0.9 1 1.1 1.2", factors)
    for (i in factors) {
        ARGV[i] = "temp2_"i".txt"
        outfiles[i] = "coord_"i".txt"
        multipliers[i] = factors[i] * original * convert
        ARGC++
    }
}
{
    printf "%-2s %10.5f %10.5f %10.5f\n", $1,
        $2 * multipliers[ARGIND],
        $3 * multipliers[ARGIND],
        $4 * multipliers[ARGIND] > outfiles[ARGIND]
}
'

If you aren't using GNU awk then add a FNR==1{ARGIND++} statement after the BEGIN{...} section. 如果您不使用GNU awk,则在BEGIN{...}部分之后添加FNR==1{ARGIND++}语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM