FIND和FINDSTR都是用来查找文件中的字符串的命令。FINDSTR支持通配符,是FIND的加强版本。
查找字符串
用空格隔开待查找的字符串"str1 str2"
,在find中表示查找str1 str1
整个字符串,比较符合直觉,
而在findstr中表示查找str
或str2
。findstr的待查字符串写成/C:"str1 str2"
表示查找整个字符串.
find1 2 3 4
| D:\test\test>find "hello there" hi.txt
---------- HI.TXT hello there
|
findstr1 2 3 4 5 6 7
| D:\test\test>findstr "hello there" hi.txt hello h"hello"h hello there
D:\test\test>findstr /C:"hello there" hi.txt hello there
|
查找的字符串中有"
在要查找的字符串里""
转义成一个"
.
find1 2 3 4
| D:\test\test>find /N "h""hello""h" hi.txt
---------- HI.TXT [6]h"hello"h
|
findstr1 2
| D:\test\test>findstr /N """hello""" hi.txt 6:h"hello"h
|
多文件查找
当前文件夹和所有子目录查找所有含有Windows
的文件,忽略大小写。
1
| findstr /s /i Windows *.*
|
search the current directory for files that have the extension .bat and that contain the string "PROMPT," type 1
| for %%f in (*.bat) do find "PROMPT" %%f
|
查找目录
用管道。
1 2
| D:\test>dir /b /s |find /i "HI" D:\test\hi.txt
|
显示文件内容并添加行号
type命令只能显示文件内容,在不使用for语句的条件下,简洁地实现打印行号的功能。
display line number1 2
| find /V /N "" filename findstr /V /N "\n" hi.txt
|
通配符查找
查找包含以任意个空格后是字符串FOR
的字符串,打印行号(/n),
1
| findstr /b /n /c:" *FOR" *.bas
|
待查字符串从文件中读取
1
| findstr /g:finddata.txt /f:filelist.txt > results.out
|
Reference:
help FIND1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 在文件中搜索字符串。
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
/V 显示所有未包含指定字符串的行。 /C 仅显示包含字符串的行数。 /N 显示行号。 /I 搜索字符串时忽略大小写。 /OFF[LINE] 不要跳过具有脱机属性集的文件。 "string" 指定要搜索的文本字符串。 [drive:][path]filename 指定要搜索的文件。
如果没有指定路径,FIND 将搜索在提示符处键入 的文本或者由另一命令产生的文本。
|
help FINDSTR1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| 在文件中寻找字符串。
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]] strings [[drive:][path]filename[ ...]]
/B 在一行的开始配对模式。 /E 在一行的结尾配对模式。 /L 按字使用搜索字符串。 /R 将搜索字符串作为一般表达式使用。 /S 在当前目录和所有子目录中搜索匹配文件。 /I 指定搜索不分大小写。 /X 打印完全匹配的行。 /V 只打印不包含匹配的行。 /N 在匹配的每行前打印行数。 /M 如果文件含有匹配项,只打印其文件名。 /O 在每个匹配行前打印字符偏移量。 /P 忽略有不可打印字符的文件。 /OFF[LINE] 不跳过带有脱机属性集的文件。 /A:attr 指定有十六进位数字的颜色属性。请见 "color /?" /F:file 从指定文件读文件列表 (/ 代表控制台)。 /C:string 使用指定字符串作为文字搜索字符串。 /G:file 从指定的文件获得搜索字符串。 (/ 代表控制台)。 /D:dir 查找以分号为分隔符的目录列表 strings 要查找的文字。 [drive:][path]filename 指定要查找的文件。
除非参数有 /C 前缀,请使用空格隔开搜索字符串。 例如: 'FINDSTR "hello there" x.y' 在文件 x.y 中寻找 "hello" 或 "there"。'FINDSTR /C:"hello there" x.y' 文件 x.y 寻找 "hello there"。
一般表达式的快速参考: . 通配符: 任何字符 * 重复: 以前字符或类出现零或零以上次数 ^ 行位置: 行的开始 $ 行位置: 行的终点 [class] 字符类: 任何在字符集中的字符 [^class] 补字符类: 任何不在字符集中的字符 [x-y] 范围: 在指定范围内的任何字符 \x Escape: 元字符 x 的文字用法 \<xyz 字位置: 字的开始 xyz\> 字位置: 字的结束
|
find in M$ website
findstr in M$ website