入门命令12-字符串查找:find

 1 C:\Documents and Settings\doupi>find /?
2 在文件中搜索字符串。
3
4 FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
5
6 /V 显示所有未包含指定字符串的行。 reverse[相反的]
7 /C 仅显示包含字符串的行数。count of lines[只返回行数,如果与其他参数合用,刚以此参数结果为准,例如同时/c /n]
8 /N 显示行号。numbers[line numbers]
9 /I 搜索字符串时忽略大小写。ignore[忽略大小写,即ignore the case]
10 /OFF[LINE] 不要跳过具有脱机属性集的文件。
11 "string" 指定要搜索的文字串,
12 [drive:][path]filename
13 指定要搜索的文件。
14
15 如果没有指定路径,FIND 将搜索键入的或者由另一命令产生的文字。

看看帮助,其实它的用法比较简单,格式比较固定:find 参数 "要查找的字符串" 要查找的文件

来看个基本用法的例子:

 1 C:\Documents and Settings\doupi>copy con test.txt
2 this is a test
3 aaa
4 bbb
5 ccc
6 aa
7 ^Z
8 已复制 1 个文件。
9 rem 普通查找,它返回的结果是以行为单位的.
10 C:\Documents and Settings\doupi>find "aa" test.txt
11
12 ---------- TEST.TXT
13 aaa
14 aa
15 rem 没找到的情况下,只返回了查找的文件名.
16 C:\Documents and Settings\doupi>find "dd" test.txt
17
18 ---------- TEST.TXT

下面,直接来看它有用参数,在篇首帮助里已经用红色标明.

 

 1 rem 结果计数
2 C:\Documents and Settings\doupi>find /c "aa" test.txt
3 ---------- TEST.TXT: 2
4 rem 结果带行号显示
5 C:\Documents and Settings\doupi>find /n "aa" test.txt
6 ---------- TEST.TXT
7 [2]aaa
8 [5]aa
9 rem 不区分大小写
10 C:\Documents and Settings\doupi>find /i "Aa" test.txt
11 ---------- TEST.TXT
12 aaa
13 aa
14 rem 返回不包含字符串的行
15 C:\Documents and Settings\doupi>find /v "bb" test.txt
16
17 ---------- TEST.TXT
18 this is a test
19 aaa
20 ccc
21 aa
22 C:\Documents and Settings\doupi>find "Aa" test.txt
23 ---------- TEST.TXT
24 rem 结果只返回了找到的记录条数
25 C:\Documents and Settings\doupi>find /c /n "aa" test.txt
26 ---------- TEST.TXT: 2

最后了再补充几点:

1.如果要查找带有如双引号之类的字符,要用""把它转义,比如只查找引号""""[引号包围的一个转义了的引号]

2.另外find支持管道,即|[所谓管道可以理解是将一个命令的结果输出作为别一个命令的输入],关于这方面的知道,后面会再有提到.

3.查找的文件可以多运用通配,如:

 

1 C:\Documents and Settings\doupi>find "aa" test*.txt
2 ---------- TEST1.TXT
3 aaa
4 aa
5 ---------- TEST2.TXT
6 aaa
7 aa

 

OK,此篇完结.

 

for /f "tokens=1,2* delims=:" %i in ('findstr "10万" *.msg') do (copy "%i" ..\)

posted @ 2011-12-13 13:01  doupip  阅读(637)  评论(0编辑  收藏  举报