Grep 和 CMD 转义
我正在尝试 grep 这种模式: ^#include\s+"[^"]+"
不幸的是,当我在 Windows 批处理文件中尝试以下操作时,管道 (|
) 和 sort
被视为 grep
的输入,可能是因为引号不平衡,
> grep -P '^#include\s+"[^"]+"' --include=*.h --include=*.cpp | sort
grep: |: No such file or directory
grep: sort: No such file or directory
我尝试了不同的转义组合,包括反斜杠和插入符,但不能。让它发挥作用。
编辑:
通过反复试验,我得到了这个工作:grep -P '^^#include\s+\"[^\"]+\^"' --include=* .h --include=*.cpp | .h --include=*.cpp | sort
它是 CMD 转义 (^
) 和正则表达式转义 (\
) 的奇怪组合,没有明显的韵律或理由需要使用在哪里。
我将这个问题悬而未决,希望有人能提供一个一般性的解释。
I am trying to grep this pattern: ^#include\s+"[^"]+"
Unfortunately, when I try the following in a Windows batch file, the pipe (|
) and the sort
are treated as inputs to grep
, probably because of the unbalanced quotes.
> grep -P '^#include\s+"[^"]+"' --include=*.h --include=*.cpp | sort
grep: |: No such file or directory
grep: sort: No such file or directory
I tried different combinations of escaping, both with backslash and with caret, but could not get it to work.
Edit:
By trial and error, I got this to work: grep -P '^^#include\s+\"[^\"]+\^"' --include=*.h --include=*.cpp | sort
It is a weird mix of CMD escapes (^
) and regex escapes (\
), without an apparent rhyme or reason to which one needs to be used where.
I am leaving the question open in the hope that someone will offer a general explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PowerShell 具有正则表达式支持。如果您使用的是受支持的 Windows 系统,则 PowerShell 已随其安装。
=== Do-Grepit.ps1
在交互式控制台中,使用别名可以减少输入次数,但不应将别名编码到脚本文件中。
如果您迫切希望从
cmd
batch-file
运行它,那么您可以:PowerShell has regex support. If you are on a supported Windows system, PowerShell was installed with it.
=== Do-Grepit.ps1
At an interactive console, using aliases could make for less typing, but aliases should not be coded into script files.
If you are desperate to run this from a
cmd
batch-file
, then you could: