为什么我不能在 gawk 中转义引号?
我正在尝试执行以下操作,但要么我太累了,无法思考,要么逃跑时发生了一些奇怪的事情:
scanimage -L | gawk '/N650U/ {print gensub("[\'`]", "", "g", $2)}'
pipe bquote>
I'm trying to do the following, but either I'm way too tired and can't think, or something weird is hapening with the escapes:
scanimage -L | gawk '/N650U/ {print gensub("[\'`]", "", "g", $2)}'
pipe bquote>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
执行此操作的习惯用法是创建一个包含单引号的变量,然后使用它:
但是,由于您在字符类中使用它,所以这是行不通的,所以您需要这样做:
另一种选择,如果使用
bash
就是使用$''
,它确实支持转义单引号在第二种情况下您所做的就是创建一个单引号在单引号之前配对,转义单引号,这样 shell 就不会解释它,然后在它后面创建另一个单引号对。
正则表达式中使用单引号的示例
正则表达式外部使用单引号的示例
$'' 内部使用单引号的示例
foo`\'' | awk /N650U/ {print gensub("[`\']", "", "g", $2)}'$''
在第二种情况下您所做的就是创建一个单引号在单引号之前配对,转义单引号,这样 shell 就不会解释它,然后在它后面创建另一个单引号对。
正则表达式中使用单引号的示例
正则表达式外部使用单引号的示例
$'' 内部使用单引号的示例
foo`\'' | awk '{gsub(/[o`'\'']/,"#")}1' f####$''
正则表达式外部使用单引号的示例
$'' 内部使用单引号的示例
/N650U/ {print gensub("[`\']", "", "g", $2)}'$''
在第二种情况下您所做的就是创建一个单引号在单引号之前配对,转义单引号,这样 shell 就不会解释它,然后在它后面创建另一个单引号对。
正则表达式中使用单引号的示例
正则表达式外部使用单引号的示例
$'' 内部使用单引号的示例
{gsub(/[o`\']/,"#")}1' f#### /N650U/ {print gensub("[`\']", "", "g", $2)}'$''
在第二种情况下您所做的就是创建一个单引号在单引号之前配对,转义单引号,这样 shell 就不会解释它,然后在它后面创建另一个单引号对。
正则表达式中使用单引号的示例
正则表达式外部使用单引号的示例
$'' 内部使用单引号的示例
foo`\'' | awk '{gsub(/[o`'\'']/,"#")}1' f####$''
正则表达式外部使用单引号的示例
$'' 内部使用单引号的示例
/N650U/ {print gensub("[`\']", "", "g", $2)}'$''
在第二种情况下您所做的就是创建一个单引号在单引号之前配对,转义单引号,这样 shell 就不会解释它,然后在它后面创建另一个单引号对。
正则表达式中使用单引号的示例
正则表达式外部使用单引号的示例
$'' 内部使用单引号的示例
$''
The idiom to do this is to create a variable which contains the single quote and then use that:
However, since you are using it in a character class, that is not going to work so you'll need to do this:
Another alternative if using
bash
is to use$''
which does support escaping single-quotesAll you are doing in the 2nd case is creating a single-quote pair right before your literal single-quote, escaping the single quote so the shell doesn't interpret it and then make another single-quote pair after it.
Example with single-quote in a regex
Example with single-quote outside a regex
Example with single-quote inside
foo`\'' | awk /N650U/ {print gensub("[`\']", "", "g", $2)}'$''
All you are doing in the 2nd case is creating a single-quote pair right before your literal single-quote, escaping the single quote so the shell doesn't interpret it and then make another single-quote pair after it.
Example with single-quote in a regex
Example with single-quote outside a regex
Example with single-quote inside
foo`\'' | awk '{gsub(/[o`'\'']/,"#")}1' f####$''
Example with single-quote outside a regex
Example with single-quote inside
/N650U/ {print gensub("[`\']", "", "g", $2)}'$''
All you are doing in the 2nd case is creating a single-quote pair right before your literal single-quote, escaping the single quote so the shell doesn't interpret it and then make another single-quote pair after it.
Example with single-quote in a regex
Example with single-quote outside a regex
Example with single-quote inside
{gsub(/[o`\']/,"#")}1' f#### /N650U/ {print gensub("[`\']", "", "g", $2)}'$''
All you are doing in the 2nd case is creating a single-quote pair right before your literal single-quote, escaping the single quote so the shell doesn't interpret it and then make another single-quote pair after it.
Example with single-quote in a regex
Example with single-quote outside a regex
Example with single-quote inside
foo`\'' | awk '{gsub(/[o`'\'']/,"#")}1' f####$''
Example with single-quote outside a regex
Example with single-quote inside
/N650U/ {print gensub("[`\']", "", "g", $2)}'$''
All you are doing in the 2nd case is creating a single-quote pair right before your literal single-quote, escaping the single quote so the shell doesn't interpret it and then make another single-quote pair after it.
Example with single-quote in a regex
Example with single-quote outside a regex
Example with single-quote inside
$''
单引号中没有特殊字符< /a> 包括反斜杠(
\
)。您可以将命令更改为:
There's no special character in single quotes including backslash(
\
).You can change the command to:
Shell '...' 不支持反斜杠转义。恐怕你必须用“...”来代替。
(请注意,shell“...”确实扩展 $ 变量,因此您也需要对其进行转义!)
Shell '...' doesn't support backslash escapes. You'll have to use "..." instead, I'm afraid.
(Note that shell "..." does expand $ variables, so you need to escape that as well!)
有点难看,但这是有效的:
A bit ugly, but this works:
正确的方法很简单:
它不会影响使用
\047
作为单引号,但顺便说一句,考虑到所有其他发布的解决方案都使用字符串分隔符,请注意正则表达式分隔符的使用而不是 gsub() 第一个参数的正则表达式周围的字符串分隔符。这很重要,例如:在正则表达式上下文中使用字符串分隔符的唯一时间是当您需要将文字与变量连接时,例如:
在手册页中查找字符串和正则表达式分隔符以获取详细信息。
The correct way to do this is simply:
It doesn't affect using
\047
for a single quote but as an aside given all of the other posted solutions are using string delimiters, note the use of regexp delimiters instead of string delimiters around the regexp that is gsub()s first argument. It matters, see for example:The only time to use string delimiters in a regexp context is when you need to concatenate a literal with a variable, e.g.:
Look up string and regexp delimiters in the man page for details.