简体   繁体   English

转义〜! 在awk(bash命令)中,反斜杠不是行中的最后一个字符

[英]Escaping ~! in awk (bash command), backslash not last character on line

I am trying to run a bash command in the following format: 我正在尝试以以下格式运行bash命令:

declare "test${nb}"="$(cat file.txt | awk '{if($3>0.5 && $3 !~ "ddf") $2="NA"; print $1,$2}')"

where $nb is an int (eg 2) and file.txt contains a table with various numeric and string values (I can provide more details if needed, but it should not be relevant here) 其中$ nb是一个整数(例如2),而file.txt包含一个具有各种数字和字符串值的表(如果需要,我可以提供更多详细信息,但是此处不应该涉及)

when running this, the shell substitutes !~ for the name of a file that I have (not sure why). 运行此命令时,shell将!〜替换为我拥有的文件名(不确定原因)。 I tried escaping this using the backslash like this: 我试图像这样使用反斜杠转义这个:

declare "test${nb}"="$(cat file.txt | awk '{if($3>0.5 && $3 \!~ "ddf") $2="NA"; print $1,$2}')"

but then I get this error: 但是然后我得到这个错误:

awk: {if($3>0.5 && $3 \!~ "ddf") $2="NA"; print $1,$2}
awk:                  ^ backslash not last character on line

I also tried having the table contained in the variable "var" and writing it this way: 我还尝试过将表包含在变量“ var”中,并以这种方式编写它:

declare test[$nb]=$(echo "$var" | awk '{if($3>0.5 && $3 !~ "ddf") $2="NA"; print $1,$2}')

Then there is no error, but the output is just the first field of first column of the table, which is not the case when I don't expand the variable name. 然后就没有错误,但是输出只是表第一列的第一字段,当我不扩展变量名时就不是这种情况。 For example, if I do this: 例如,如果我这样做:

declare test2=$(echo "$var" | awk '{if($3>0.5 && $3 !~ "ddf") $2="NA"; print $1,$2}')

then it works perfectly and test2 has the expected value. 那么它可以完美工作,并且test2具有预期值。 But I need to be able to use any number instead of 2 (something like test[$nb]). 但是我需要能够使用任何数字而不是2(类似于test [$ nb])。

any idea how I could fix this? 知道我该如何解决吗? Any help will be very appreciated! 任何帮助将不胜感激!

thanks 谢谢

Lose the quotes: 失去引号:

$ declare x="$( echo '!' )"
-bash: !': event not found
$ declare x=$( echo '!' )
$ echo "$x"
!

You have a lot of other issues with your statement, though, including UUOC, using a scalar to emulate an array, non idiomatic awk syntax, etc. Try this instead: 但是,您的语句还有很多其他问题,包括UUOC,使用标量来模拟数组,非惯用的awk语法等。请尝试以下操作:

declare test[$nb]=$( awk '{print $1, (($3 > 0.5) && ($3 !~ /ddf/) ? "NA" : $2)}' file.txt )

Are you talking about interactively entering a command, or running the command from a file (ie script)? 您是在交互地输入命令,还是从文件 (即脚本)运行命令? The history expansion of !~ occurs only in interactive use. !〜的历史扩展仅在交互使用中发生。 BTW, the bash manpage says: If enabled, history expansion will be performed unless an ! 顺便说一句,bash的联机帮助页上说: 如果启用,除非执行!,否则将进行历史扩展。 appearing in double quotes is escaped using a backslash. 出现在双引号中的使用反斜杠进行转义。 The backslash preceding the ! !前面的反斜杠 is not removed. 删除。

This means that, as long as you do scripting (or, more precisley, as long as you do not have an interactive shell), you don't have to worry about this special meaning of '!'. 这意味着,只要您编写脚本(或更精确地说,只要您没有交互式外壳),您就不必担心“!”的特殊含义。

If you have an interactive shell and history expansion bothers you, you can turn it off by 如果您有一个交互式外壳,并且历史记录扩展困扰您,则可以通过以下方式将其关闭

set +H

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

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