awk:警告:转义序列'\]'被视为普通的']' [英] awk: warning: escape sequence `\]' treated as plain `]'

查看:602
本文介绍了awk:警告:转义序列'\]'被视为普通的']'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

输出:

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.

我想同时使用定界符:\并通过打印$1$2$3$4来精确打印字母ABCD分别.

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

推荐答案

我认为这是您要尝试做的事情:

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

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

请注意,我添加了[[:space:]]*,因为我认为您不希望在$1之后的字段中使用前导空格.

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

在您的FS设置中使用的字符串文字会被解析两次,一次是在读取脚本时执行,另一次是在执行时执行,因此转义符需要加倍(使用Google和/或阅读awk手册页或书).在这种情况下,第一次将\\\\转换为\\,在第二次通过\\转换为\,这就是您想要的.

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.

这篇关于awk:警告:转义序列'\]'被视为普通的']'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆