windows Windows批处理文件中的grep
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19057751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep in windows batch file
提问by Steve Cohen
I am trying to write a Windows batch file that will look through a specific html index file that looks something like this (simplified)
我正在尝试编写一个 Windows 批处理文件,该文件将查看看起来像这样的特定 html 索引文件(简化)
<a href=emergency.htm>Emergency Calls</a><br>
<a href=EmeRgency.htm>Emergency Calls</a><br>
<a href=Emergency.htm>Emergency Calls</a><br>
<a href=EMERGENCY.htm>Emergency Calls</a><br>
<a href=E911.htm>Emergency Calls</a><br>
<a href=e911.htm>Emergency Calls</a><br>
and print all links whose filenames contain any uppercase letters so that they may be corrected not to so include any.
并打印文件名包含任何大写字母的所有链接,以便可以更正它们以不包含任何大写字母。
The following works in unix:
以下在 unix 中工作:
$ grep -v '^<a href=[^A-Z]*\.htm' helpindex.htm
<a href=EmeRgency.htm>Emergency Calls</a><br>
<a href=Emergency.htm>Emergency Calls</a><br>
<a href=EMERGENCY.htm>Emergency Calls</a><br>
<a href=E911.htm>Emergency Calls</a><br>
(the -v reverses the match)
( -v 反转匹配)
But using the UnxUtils grep under Windows, which is a direct port of unix grep, I can't come up with a way of quoting the regex that works. This would be necessary to use it in a batch file. I've tried ', " with no joy and also the -E switch. Is there any way to do this using this particular toolset?
但是在 Windows 下使用 UnxUtils grep,它是 unix grep 的直接端口,我想不出一种引用有效正则表达式的方法。这对于在批处理文件中使用它是必要的。我试过 ', " 没有任何乐趣,还有 -E 开关。有没有办法使用这个特定的工具集来做到这一点?
@janos led me to the findstr command in Windows but it still doesn't work. Looking at the findstr help I see:
@janos 引导我使用 Windows 中的 findstr 命令,但它仍然不起作用。查看 findstr 帮助我看到:
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]] strings [[drive:][path]filename[ ...]]
...
/V Prints only lines that do not contain a match. ...
/C:string Uses specified string as a literal search string. ...Use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y.
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [ /F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]] strings [[drive:][path]filename[ . ..]]]
...
/V 仅打印不包含匹配项的行。...
/C:string 使用指定的字符串作为文字搜索字符串。...除非参数以 /C 为前缀,否则请使用空格分隔多个搜索字符串。例如,'FINDSTR "hello there" xy' 在文件 xy 中搜索“hello”或“there” 'FINDSTR /C:"hello there" xy' 在文件 xy 中搜索“hello there”
However, this doesn't work either:
但是,这也不起作用:
C:\home\sftp>findstr /V /C:"^<a href=[^A-Z]*\.htm" helpindex.htm
<a href=emergency.htm>Emergency Calls</a><br>
<a href=EmeRgency.htm>Emergency Calls</a><br>
<a href=Emergency.htm>Emergency Calls</a><br>
<a href=EMERGENCY.htm>Emergency Calls</a><br>
<a href=E911.htm>Emergency Calls</a><br>
<a href=e911.htm>Emergency Calls</a><br>
Either findstr is garbage or there is some subtle difference from grep.
findstr 要么是垃圾,要么与 grep 有一些细微的差别。
回答by dbenham
This works fine for me in Windows command console:
这在 Windows 命令控制台中对我来说很好用:
grep -v "^<a href=[^A-Z]*\.htm" helpindex.htm
FINDSTR does not work with [^A-Z]
because it uses a non-standard collation sequence: See Why does findstr not handle case properly (in some circumstances)?
FINDSTR 不起作用,[^A-Z]
因为它使用非标准的整理序列:请参阅为什么 findstr 不能正确处理大小写(在某些情况下)?
You can use FINDSTR to get your desired output using:
您可以使用 FINDSTR 来获得所需的输出:
findstr /rvc:"^<a href=[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]*\.htm" helpindex.htm
The /C option is needed to force the entire string to be considered one search term.
/C 选项用于强制将整个字符串视为一个搜索词。
The /R option is needed to force interpretation of the search term as a regex. The default for the /C option is a string literal.
需要 /R 选项来强制将搜索词解释为正则表达式。/C 选项的默认值是字符串文字。
You might want to have a look at What are the undocumented features and limitations of the Windows FINDSTR command?. There is a long list of "gotchas"
您可能想看看Windows FINDSTR 命令有哪些未记录的功能和限制?. 有一长串“陷阱”
Edit
编辑
UnxUtils is an old, outdated distribution of GNU unix utilities for Windows. You should get newer releases from GNU Coreutils: see Difference between UnxUtils and GNU CoreUtils
UnxUtils 是用于 Windows 的 GNU unix 实用程序的旧的、过时的发行版。您应该从 GNU Coreutils 获得更新的版本:请参阅UnxUtils 和 GNU CoreUtils 之间的差异
I believe I got my distribution of GNU Coreutils from http://gnuwin32.sourceforge.net/packages/coreutils.htm. I'm not sure if that is the most up-to-date package, but it should solve your grep problem. It provides a convenient package of many utilities.
我相信我从http://gnuwin32.sourceforge.net/packages/coreutils.htm获得了我的 GNU Coreutils 发行版。我不确定这是否是最新的软件包,但它应该可以解决您的 grep 问题。它提供了许多实用程序的方便包。
Another option is to get individual GNU utilities for Windows from http://gnuwin32.sourceforge.net/packages.html
另一种选择是从http://gnuwin32.sourceforge.net/packages.html获取 Windows 的单个 GNU 实用程序
回答by Aacini
You may use my FindRepl.bat program that works as you want. For example:
您可以根据需要使用我的 FindRepl.bat 程序。例如:
> type helpindex.htm
<a href=emergency.htm>Emergency Calls</a><br>
<a href=EmeRgency.htm>Emergency Calls</a><br>
<a href=Emergency.htm>Emergency Calls</a><br>
<a href=EMERGENCY.htm>Emergency Calls</a><br>
<a href=E911.htm>Emergency Calls</a><br>
<a href=e911.htm>Emergency Calls</a><br>
> FindRepl /V "^<a href=[^A-Z]*\.htm" < helpindex.htm
<a href=EmeRgency.htm>Emergency Calls</a><br>
<a href=Emergency.htm>Emergency Calls</a><br>
<a href=EMERGENCY.htm>Emergency Calls</a><br>
<a href=E911.htm>Emergency Calls</a><br>
You may download FindRepl.bat from this site
您可以从该站点下载 FindRepl.bat