cmd findstr 字符串查找增强使用说明

2019-01-30 05:22:02于海丽

C:UsershelloworldDesktop>findstr /n /i "^hello$" a.txt

结果让你倍感欣喜:1:hello
其实除了这一种方法外,findstr命令还提供了/x参数用来查找完全匹配的行。
代码:

C:UsershelloworldDesktop>findstr /n /i /x "hello" a.txt

结果:

1:hello

6.关闭正则表达式会怎么样?
我们可以人为地将findstr分为两种模式,“正则表达式模式”和“普通字符串模式”。
findstr默认为“正则表达式模式”,加上/r参数也是“正则表达式模式”(换言之,/r参数有点多余)。
加上/l参数后,findstr转换为“普通字符串模式”(其实find就是这种模式、且只有这种模式)。
“普通字符串模式”下,以同样的代码,看看结果怎样?
代码:

C:UsershelloworldDesktop>findstr /li "^hello" a.txt

结果什么都没显示出来。
以hello开头的行明明有以下这些,为什么没显示出来呢?

hello hello
Hello World
Hello Boy
hello ,good man.

因为,当你使用“普通字符串模式”,findstr不会把^当做是正则表达式的元字符,而只是把其当做普通字符^,也就是说它此时已经不具备“表示行首”的功能,变成了和h之类字符一样的普通民众,再也没“特权”。
改变a.txt的内容:^hello

hello
hello hello
good hello
你好 hello world
Hello World
Hello Boy
hello ,good man.
goodbye!

再次运行代码:

C:UsershelloworldDesktop>findstr /nli "^hello" a.txt

结果:

1:^hello

7.查找不包含指定字符的行
如果比较一下find和findstr命令就会发现,他们都具有/v,/n,/i,/off[line]参数,而且功能都是一摸一样的,这里说的就是/v参数。
查找不包含hello的行。
代码:

C:UsershelloworldDesktop>findstr /vni "hello" a.txt

结果:

9:goodbye!

8.如何查找当前目录及子目录下文件内容中包含某字符串的文件名?
在写这篇教程的时候,偶然看到有批友问了这个问题,问题地址:http://bbs.bathome.net/viewthread.php?tid=14727
代码:

findstr /ms "专业" *.txt

效果:
找出当前目录及子目录下文件内容中包含“专业”的文本文件,并只显示其文件名。
相关文章 大家在看