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

2019-09-19 06:51:10于丽


C:UsershelloworldDesktop>findstr /ig:string.txt a.txt

效果:

hello
hello hello
Hello World
Hello Boy
hello ,good man.

被忽略的行

^hello
good hello
你好 hello
goodbye!

从被忽略的“^hello”可以看出,在不加/l参数的前提下,用/g指定的搜索字符串中如果含有“元字符”,则作为正则表达式使用,而不是作为普通表达式。
10.搜索一个完全匹配的句子
其实findstr自带的帮助中就有个很好的例子:
例如: 'FINDSTR "hello there" x.y' 在文件 x.y 中寻找 "hello" 或
"there"。'FINDSTR /C:"hello there" x.y' 文件 x.y 寻找
"hello there"。
可以以这个例子来做个测试。

a.txthello there
hellothere
hello
there

代码:

C:UsershelloworldDesktop>findstr /ic:"hello there" a.txt

结果:

hello there

这就是句子的完全匹配了。
11.搜索一个完全匹配的词。
这里也涉及到了两个元字符:<,>。
先试看一个例子。
a.txt

far there
farthere
there
far
farm
farmer

代码:

C:UsershelloworldDesktop>findstr "far" a.txt

结果:

far there
farthere
far
farm
farmer

我的本意是要查找含有“far”这个单词的行,但是farthere、farm、farmer却显示出来了,这不是我想要的结果。
如果只要求显示含有“far”这个单词的行,该怎么写呢?
代码:

C:UsershelloworldDesktop>findstr "<far>" a.txt

结果:

far there
far

12.指定要查找的目录
/d参数我一直把它和/f、/g归为一类,但其实二者截然不同,/f、/g是用文本文件制定要查找的文件、字符串,而/d是直接书写目录名到命令中。
代码:

C:UsershelloworldDesktop>findstr /imd:520;编程; ".*" "*.txt"

结果:

520:
hello.txt

编程:
help.txt

win7 help比xp help多出来的命令.txt
wmic.txt

查找在520、编程目录中所有包含任意字符的txt文件。
13.统计字符数
/o:在每行前打印字符偏移量,在找到的每行前打印该行首距离文件开头的位置,也就是多少个字符,如test.txt中有如下内容:

aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa

执行命令:findstr /o .* test.txt
复制代码::上一行中的.*为正则表达式的内容,表示任意行,包含空行
结果如下:

0:aaaaaaaaaa
12:aaaaaaaaaa
24:aaaaaaaaaa
36:aaaaaaaaaa
48:aaaaaaaaaa

注意每行末尾的回车换行符算两个字符。
相关文章 大家在看