阅读背景:

awk:警告:转义序列' \ '被视为plain ']

来源:互联网 
echo "A:\ B:\ C:\ D:\" | awk -F'[:\]' '{print 
echo "A:\ B:\ C:\ D:\" | awk -F'[:\]' '{print $1}'

Output:

输出:

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

A

I am getting the above warning message, I have tried multiple options to escape the ] but that didn't resolve.

我收到了上面的警告信息,我尝试了多个选项来逃避,但是没有解决。

I want to use both the delimiters :\ and print exactly the alphabets A, B, C, D by printing $1, $2, $3, $4 respectively.

我想要使用两个分隔符:\并打印一个字母A、B、C、D,分别打印$1、$2、$3、$4。

2 个解决方案

#1


6  

I think this is what you're trying to do:

我想这就是你想做的:

$ echo 'A:\ B:\ C:\ D:\' | awk -F':\\\\[[:space:]]*' '{print $2}'
B

Note that I added [[:space:]]* as I don't think you want leading spaces in the fields after $1.

请注意,我添加了[[:空格:]]*,因为我认为您不希望在$1之后的字段中有领先的空格。

String literals as used in your FS setting get parsed twice, once when the script is read and again when executed so escapes need to be doubled up (google it and/or read the awk man page or book). In this case on the first pass \\\\ gets converted to \\ and on the second pass \\ gets converted to \ which is what you want.

在FS设置中使用的字符串文本将被解析两次,一次是在读取脚本时,另一次是在执行时,因此需要将转义加倍(谷歌和/或读取awk手册页或书)。在这种情况下,在第一个过路\\\\被转换成\\\,在第二个过路\\被转换成\,这是你想要的。

#2


0  

This is also wrong:

这也是错误的:

echo "A:\ B:\ C:\ D:\"

You are escaping the " and it breaks the input, you need to remove it or use single quote.

你正在逃离“它会打断输入,你需要删除它或者使用单引号。”

awk does not like a single \ within field separator, so try:

awk不喜欢字段分隔符内的单个\,所以请尝试:

echo 'A:\ B:\ C:\ D:\' | awk -F":|\\" '{print $1}'
A

Or you can escape it (single quote):

或者你也可以转义(单引语):

echo 'A:\ B:\ C:\ D:\' | awk -F'[:\\\\]' '{print $1}'
A

Or even more escaping (double quote):

或者更多的转义(双引号):

echo 'A:\ B:\ C:\ D:\' | awk -F"[:\\\\\\\]" '{print $1}'
A

}' echo "A:\ B:\ C:\ D:\" | awk -F'[:\]' '{print $



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: