简体   繁体   English

如何在PowerShell中的ACK中转义双引号和冒号

[英]How to escape double quotes and colon in ACK in PowerShell

I am using Ack version 2.04, in PowerShell.我在 PowerShell 中使用 Ack 2.04 版。 I want to search texts like "jsonClass":"Page" (quotes included) inside text files.我想在文本文件中搜索诸如“jsonClass”:“Page” (包括引号)之类的文本。

I can't seem to get the quoting and escaping correct.我似乎无法正确引用和转义。

ack -c --match '"jsonClass":"Page"'

It doesn't work in PowerShell.它在 PowerShell 中不起作用。 I guess ack's picking up the single quotes as well.我猜 ack 也会选择单引号。

Escaping the double quotes gives an invalid regex error:转义双引号会产生无效的正则表达式错误:

ack -c --match "\"jsonClass\":\"Page\""
  Invalid regex '\':
  Trailing \ in regex m/\/ at C:\CHOCOL~1\lib\ACK2~1.04\content\ack.pl line 315

I tried the literal option as well, but I think ack's interpreting the colon as file parameters.我也尝试了文字选项,但我认为 ack 将冒号解释为文件参数。

ack -c -Q --match "jsonClass":"Page"
  ack.pl: :Page: No such file or directory

What am I missing?我错过了什么?

I am using PowerShell v2.我正在使用 PowerShell v2。

To complement JPBlanc's effective answer with a PowerShell v3+ solution:要使用PowerShell v3+解决方案补充JPBlanc 的有效答案

When invoking external programs such as ack , use of the so-called stop-parsing symbol, --% , makes PowerShell pass the remaining arguments through as-is , with the exception of expanding cmd.exe -style environment-variable references such as %PATH% :调用ack外部程序时,使用所谓的停止解析符号--%使 PowerShell按原样传递剩余参数扩展cmd.exe风格的环境变量引用除外,例如%PATH% :

ack --% -c --match "\"jsonClass\":\"Page\""

This allows you to focus on the escaping rules of the target program only , without worrying about the complex interplay with PowerShell's own parsing and escaping.使您可以只关注目标程序的转义规则,而不必担心与 PowerShell 自己的解析和转义的复杂相互作用。

Thus, in PowerShell v3 or higher, the OP's own 2nd solution attempt would have worked by passing --% as the first parameter.因此,在 PowerShell v3 或更高版本中,OP 自己的第二个解决方案尝试将通过传递--%作为第一个参数来工作。

SeeGet-Help about_Parsing .请参阅Get-Help about_Parsing


Note that the exact equivalent of the above command without the use of --% (that also works in PSv2 and is generally helpful if you want to include PowerShell-expanded variables / expressions in other arguments ) would look like this:请注意,不使用--%的上述命令的完全等效项(这也适用于PSv2如果您想在其他参数中包含 PowerShell 扩展变量/表达式,通常会很有帮助)如下所示:

ack -c --match '"\"jsonClass\":\"Page\""'

That is, the entire argument to be passed as-is is enclosed in single quotes , which ensures that PowerShell doesn't interpret it.也就是说,要按原样传递整个参数用引号括起来,以确保 PowerShell 不会对其进行解释。

Note the inner enclosing " that aren't present in JPBlanc's answer (as of this writing). They guarantee that the argument is ultimately seen as a single argument by ack , even if it contains whitespace.请注意 JPBlanc 的答案中不存在的内部封闭" (在撰写本文时)。它们保证该参数最终被ack视为单个参数,即使它包含空格。

If ack is a function it could happen that -c is interpreted by PowerShell.如果ack是一个函数,那么-c可能会被 PowerShell 解释。 Can you test the following?您可以测试以下内容吗?

ack '-c' '--match' '"jsonClass":"Page"'

-c --match should be interpreted as --match is the value of parameter c in function ask . -c --match应该被解释为--match是函数ask中参数c的值。

If it's an EXE file, try:如果是 EXE 文件,请尝试:

ack -c --match '\"jsonClass\":\"Page\"'

In Powershell you can always use ' quote to handle inner " without problems.在 Powershell 中,您始终可以使用' quote 来处理内部"没有问题。

But if you need to use " for a string (to interpolate a variable, for instance), you can escape them with `但是,如果您需要将"用于字符串(例如插入变量),您可以使用 ` 转义它们

$myvar = 'test'
$formatted = "{`"value`":`"$myvar`"}"

Write-Host $formatted
# output: {"value":"test"}

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

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