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

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

/n,全英文number,意为“数字”;引申为“行号”;
/m,merely,意为“只是”;
/o,offset,意为“偏移”;
/p,print,意为“打印”;
/off[line],意为“脱机文件”;
/a,attribute,意为“属性”;
/f,file,意为“文件”;
/c,case,意为“把几个字加起来”;引申为“全部字匹配”;
/g,get,意为“获得”;
/d,directory,意为“目录”;
class,类。

感谢HAT的单词提供。

感谢weichengxiehou。

参数详解部分13-14节都是从weichengxiehou的帖子里复制来的(既然有现成了,省心多少),原帖地址。


参数详解:
学习findstr需要大量的实践体会,所以需要新建一些txt文本以供测试。

a.txt的内容(a.txt的内容在后面会多次修改,请注意!):

Hello World
Hello Boy
hello ,good man.
goodbye!

1.最简单的应用:在指定文本中查找指定字符串
代码:

findstr "hello" a.txt

结果:

C:UsershelloworldDesktop>findstr "hello" a.txt
hello ,good man.

代码:

findstr "Hello" a.txt

结果:

C:UsershelloworldDesktop>findstr "Hello" a.txt
Hello World
Hello Boy

这里可以看出,
findstr默认是区分大小写的(跟find命令一样)——找hello就不会出现Hello,反之亦然。
怎么让其不区分大小写呢?
用/i参数!
例如:

C:UsershelloworldDesktop>findstr /i "Hello" a.txt
Hello World
Hello Boy
hello ,good man.

2.显示要查找的字符具体在文本哪一行
代码:C:UsershelloworldDesktop>findstr /n /i "hello" a.txt
复制代码效果:

1:Hello World
2:Hello Boy
3:hello ,good man.

显示的结果中冒号(:)是英文格式下的,在用for提取的时候需要注意!
这里可以对比一下find命令的/n参数:
代码:

C:UsershelloworldDesktop>find /n "hello" a.txt

效果:---------- A.TXT
[3]hello ,good man.
复制代码冒号(:)和中括号([]),这就是差别,编写代码的时候一定要注意。
3.查找包含了指定字符的文本
代码:

C:UsershelloworldDesktop>findstr /m /i "hello" *.txt

效果:
相关文章 大家在看