为什么我不能在 gawk 中转义引号?

发布于 2025-01-02 14:02:19 字数 158 浏览 1 评论 0原文

我正在尝试执行以下操作,但要么我太累了,无法思考,要么逃跑时发生了一些奇怪的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无法回应 2025-01-09 14:02:19

执行此操作的习惯用法是创建一个包含单引号的变量,然后使用它:

scanimage -L | gawk '/N650U/ {print gensub(q"`", "", "g", $2)}' q="'"

但是,由于您在字符类中使用它,所以这是行不通的,所以您需要这样做:

scanimage -L | gawk '/N650U/ {print gensub("[`'\'']", "", "g", $2)}'
                    <--      1st pair       -->  <--   2nd pair  -->

另一种选择,如果使用 bash 就是使用 $'' ,它确实支持转义单引号

scanimage -L | gawk 

在第二种情况下您所做的就是创建一个单引号在单引号之前配对,转义单引号,这样 shell 就不会解释它,然后在它后面创建另一个单引号对。

正则表达式中使用单引号的示例

$ echo 

正则表达式外部使用单引号的示例

$ echo "foo" | awk '{print q$0q}' q="'"
'foo'

$'' 内部使用单引号的示例 $''

echo 
/N650U/ {print gensub("[`\']", "", "g", $2)}'

在第二种情况下您所做的就是创建一个单引号在单引号之前配对,转义单引号,这样 shell 就不会解释它,然后在它后面创建另一个单引号对。

正则表达式中使用单引号的示例


正则表达式外部使用单引号的示例


$'' 内部使用单引号的示例 $''


foo`\'' | awk '{gsub(/[o`'\'']/,"#")}1'
f####

正则表达式外部使用单引号的示例


$'' 内部使用单引号的示例 $''


/N650U/ {print gensub("[`\']", "", "g", $2)}'

在第二种情况下您所做的就是创建一个单引号在单引号之前配对,转义单引号,这样 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:

scanimage -L | gawk '/N650U/ {print gensub(q"`", "", "g", $2)}' q="'"

However, since you are using it in a character class, that is not going to work so you'll need to do this:

scanimage -L | gawk '/N650U/ {print gensub("[`'\'']", "", "g", $2)}'
                    <--      1st pair       -->  <--   2nd pair  -->

Another alternative if using bash is to use $'' which does support escaping single-quotes

scanimage -L | gawk 

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

$ echo 

Example with single-quote outside a regex

$ echo "foo" | awk '{print q$0q}' q="'"
'foo'

Example with single-quote inside $''

echo 
/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 $''

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 $''

紫南 2025-01-09 14:02:19

单引号中没有特殊字符< /a> 包括反斜杠(\)。

将字符括在单引号 (') 中可保留引号内每个字符的字面值。单引号之间不能出现单引号,即使前面有反斜杠也是如此。

您可以将命令更改为:

$ scanimage -L | awk '/N650U/ {print gensub("['"'"'`]", "", "g", $2)}'

There's no special character in single quotes including backslash(\).

Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

You can change the command to:

$ scanimage -L | awk '/N650U/ {print gensub("['"'"'`]", "", "g", $2)}'
牵你的手,一向走下去 2025-01-09 14:02:19

Shell '...' 不支持反斜杠转义。恐怕你必须用“...”来代替。

gawk "/N650U/ {print gensub(\"['`]\", \"\", \"g\", \$2)}\"

(请注意,shell“...”确实扩展 $ 变量,因此您也需要对其进行转义!)

Shell '...' doesn't support backslash escapes. You'll have to use "..." instead, I'm afraid.

gawk "/N650U/ {print gensub(\"['`]\", \"\", \"g\", \$2)}\"

(Note that shell "..." does expand $ variables, so you need to escape that as well!)

铜锣湾横着走 2025-01-09 14:02:19
gawk '/N650U/ {print gensub("[\'`]", "", "g", $2)}'
     ^                         ^
     beginning of sq string       end of single quoted string

有点难看,但这是有效的:

gawk '/N650U/ {print gensub("['"'"'`]", "", "g", $2)}'
gawk '/N650U/ {print gensub("[\'`]", "", "g", $2)}'
     ^                         ^
     beginning of sq string       end of single quoted string

A bit ugly, but this works:

gawk '/N650U/ {print gensub("['"'"'`]", "", "g", $2)}'
遇到 2025-01-09 14:02:19

正确的方法很简单:

scanimage -L | gawk '/N650U/ {print gensub(/[\047`]/, "", "g", $2)}'

它不会影响使用 \047 作为单引号,但顺便说一句,考虑到所有其他发布的解决方案都使用字符串分隔符,请注意正则表达式分隔符的使用而不是 gsub() 第一个参数的正则表达式周围的字符串分隔符。这很重要,例如:

$ printf 'a\\tb\n' | awk '{sub(/\\t/,"X")}1'
aXb
$ printf 'a\\tb\n' | awk '{sub("\\t","X")}1'
a\tb
$ printf 'a\\tb\n' | awk '{sub("\\\\t","X")}1'
aXb

在正则表达式上下文中使用字符串分隔符的唯一时间是当您需要将文字与变量连接时,例如:

awk '{sub("<"var">","")}1'

在手册页中查找字符串和正则表达式分隔符以获取详细信息。

The correct way to do this is simply:

scanimage -L | gawk '/N650U/ {print gensub(/[\047`]/, "", "g", $2)}'

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:

$ printf 'a\\tb\n' | awk '{sub(/\\t/,"X")}1'
aXb
$ printf 'a\\tb\n' | awk '{sub("\\t","X")}1'
a\tb
$ printf 'a\\tb\n' | awk '{sub("\\\\t","X")}1'
aXb

The only time to use string delimiters in a regexp context is when you need to concatenate a literal with a variable, e.g.:

awk '{sub("<"var">","")}1'

Look up string and regexp delimiters in the man page for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文