我想搜索某个目录中的所有文件以查找语句的出现
Load frmXYZ
Run Code Online (Sandbox Code Playgroud)
我在Windows 7上使用该findstr命令.我试过了:
findstr /n Load.*frm *.*
Run Code Online (Sandbox Code Playgroud)
但这给了我不想要的结果,例如:
If ABCFormLoaded Then Unload frmPQR
Run Code Online (Sandbox Code Playgroud)
所以我试图在它之间放一个空格Load,frm并给出这样的命令:
findstr /n Load frm *.*
Run Code Online (Sandbox Code Playgroud)
但这只是搜索了所有单词load或所有单词的出现次数frm.我该如何解决这个问题?
小智 30
如果使用空格,则需要/C:选择将文字字符串传递给正则表达式/R选项.
一旦它进入正则表达式,它被视为正则表达式.
也就是说,这是典型的MS垃圾.
最重要的是你必须使用2个字符串来处理
Load frm开头的情况,如下所示:
Load frm apples bananas carrots或者在中间如此:
some other text Load frm and more.下面是使用XP sp3,windows 7可能会有所不同,两者都是垃圾!
findstr /N /R /C:" *Load *frm" /C:"^Load *frm" test.txt
7:Load frm is ok
8: Load frm is ok
Run Code Online (Sandbox Code Playgroud)
注意:冒号/C:是MANDATORY,以便工作.
如果你省略冒号,那么findstr错误处理只是/C作为一个无效的选项,忽略那个无效的选项,然后继续.导致意外和不需要的输出.
findstr /N /R /C:"[ ][ ]*Load[ ][ ]*frm" /C:"^Load[ ][ ]*frm" test.txt
// The first regex search string breaks down like this:
[ ] // require 1 space
[ ]* // optional many spaces
Load // literal 'Load'
[ ] // require 1 space
[ ]* // optional many spaces
frm // literal 'frm'
// The second regex search string breaks down like this:
^ // beginning of line
Load // literal 'Load'
[ ] // require 1 space
[ ]* // optional many spaces
frm // literal 'frm'
Run Code Online (Sandbox Code Playgroud)
一个真正的正则表达式可能是 \bLoad\s+frm
Joe*_*oey 24
使用/c选项:
findstr /n /c:"Load frm" *.*
Run Code Online (Sandbox Code Playgroud)
来自help(findstr /?):
/C:string Uses specified string as a literal search string.
Run Code Online (Sandbox Code Playgroud)
我使用了特殊的\<“词首”正则表达式符号。
我在 findstr 的 Win10 版本上试过这个。但据微软称,这个特殊\<符号findstr.exe自 WinXP 以来一直存在。
许多在下面不起作用的选项的完整(和痛苦的)分解。
在最底层:什么真正有效。
C:\>type lines.txt
Load frmXYZ // This line should match.
If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
pears Load frm grapes pineapples // This line should match.
// This blank line should NOT match.
LOAD FRMXYZ // This line should match.
IF ABCFORMLOADED THEN UNLOAD FRMPQR // This line should NOT match.
PEARS LOAD FRM GRAPES PINEAPPLES // This line should match.
// This blank line should NOT match.
load frmxyz // This line should match.
if abcformloaded then unload frmpqr // This line should NOT match.
pears load frm grapes pineapples // This line should match.
Run Code Online (Sandbox Code Playgroud)
C:\>type lines.txt | findstr /N "Load frm"
1:Load frmXYZ // This line should match.
2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
3:pears Load frm grapes pineapples // This line should match.
9:load frmxyz // This line should match.
10:if abcformloaded then unload frmpqr // This line should NOT match.
11:pears load frm grapes pineapples // This line should match.
Run Code Online (Sandbox Code Playgroud)
C:\>type lines.txt | findstr /N /R "Load frm"
1:Load frmXYZ // This line should match.
2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
3:pears Load frm grapes pineapples // This line should match.
9:load frmxyz // This line should match.
10:if abcformloaded then unload frmpqr // This line should NOT match.
11:pears load frm grapes pineapples // This line should match.
Run Code Online (Sandbox Code Playgroud)
C:\>type lines.txt | findstr /N /R /C:"Load frm"
1:Load frmXYZ // This line should match.
3:pears Load frm grapes pineapples // This line should match.
Run Code Online (Sandbox Code Playgroud)
C:\>type lines.txt | findstr /N /R /I /C:"Load frm"
1:Load frmXYZ // This line should match.
2:If ABCFormLoaded Then Unload frmPQR // This line should NOT match.
3:pears Load frm grapes pineapples // This line should match.
5:LOAD FRMXYZ // This line should match.
6:IF ABCFORMLOADED THEN UNLOAD FRMPQR // This line should NOT match.
7:PEARS LOAD FRM GRAPES PINEAPPLES // This line should match.
9:load frmxyz // This line should match.
10:if abcformloaded then unload frmpqr // This line should NOT match.
11:pears load frm grapes pineapples // This line should match.
Run Code Online (Sandbox Code Playgroud)
C:\>type lines.txt | findstr /N /R /C:"\<Load frm"
1:Load frmXYZ // This line should match.
3:pears Load frm grapes pineapples // This line should match.
Run Code Online (Sandbox Code Playgroud)
C:\>type lines.txt | findstr /N /R /I /C:"\<Load frm"
1:Load frmXYZ // This line should match.
3:pears Load frm grapes pineapples // This line should match.
5:LOAD FRMXYZ // This line should match.
7:PEARS LOAD FRM GRAPES PINEAPPLES // This line should match.
9:load frmxyz // This line should match.
11:pears load frm grapes pineapples // This line should match.
Run Code Online (Sandbox Code Playgroud)