- ·上一篇文章:用call对变量进行截取
- ·下一篇文章:批处理再现九九乘法表
- ·百度中搜索更多的关于“查找命令find & findstr”相关内容
- ·谷歌中搜索更多的关于“查找命令find & findstr”相关内容
- ******申明******
- 本站文章内容有部分为收录网络中其他网友内容,DOS资源站不保证所有的代码都适合你使用。
- 由于编辑匆忙,有可能造成某些脚本文件出现丢失代码或代码无法运行的情况,请网友根据情况自行修改。
- 如果能将出错部分反馈给我,那就更好了。
查找命令find & findstr
find
Searches for a text string in a file or files.
FIND [/V] [/C] [/N] [/i] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
/OFF[LINE] Do not skip files with offline attribute set.
"string" Specifies the text string to find.
[drive:][path]filename
Specifies a file or files to search.
If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.
如果没有指定路径,FIND 将搜索键入的或者由另一命令产生的文字。
使用find [参数] "?" 文件
查找文件后到结果总会在首行来个(表示下面的内容是来自*.*这个文件)
---------- *.*
当/v /c 参数连用时是(n为整数)
---------- *.*: n
现在来说参数的用法:
/v参数 不含"str"的行 如帮助所说,显示不包含指定字符串的行
find /v "?" ... 显示不包含?的行
但是常用find /v "" ...
它是打印文件所有内容,因为文件不会包含空字符.
/c 参数count 行数不会显示内容,只显示包含搜索字符串的行数,
这里要理解find在分析一个文件时它将回车换行符作为标志来认定为一行的结束与下行的开始,
find /c "?"... 不显示含?的行,但显示含?的行数,
find /c /v "?" ... 不显示行的内荣, 显示不包含?的行的行数,
find /c /v "" ... 仅显示不含空字符的行的行数,即文件的总行数.
/n参数 nember of lines若显示行的内容则给行首带上行号[:n]
/n总是与/v连用时才能有效,与/c参数连用是无效.
/i 参数Ignores 对?忽略大小写.
若"string"含有空格,则空格不会被解释为逻辑或,而是有效字符串
find 参数 "?" *.txt *.c在当前路径的所有txt文件里进行[参数]查找(文件也可以枚举的格式:1.txt 2.txt 3.txt)
常用格式:
find /c "?" *.txt *.bat
find /v "?" *.txt *.bat
find /n "?" 1.txt
find /c /v "" *.*
findstr
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 Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurances of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.
最多搜索字符串字节数为127
一般表达式举例:
1.
findstr . 2.txt 或 findstr "." 2.txt
从文件2.txt中查找任意字符(包括空格),不包括空字符或空行(过滤空行),此用法对unicode的字符也有效,下同.
2.
findstr .* 2.txt 或 findstr ".*" 2.txt
从文件2.txt中查找任意字符包括空行(原样打印)
3.
findstr "[0-9]" 2.txt
从文件2.txt中查找包括数字0-9的字符串或行
4.
findstr "[a-zA-Z]" 2.txt
从文件2.txt中查找包括任意字符(52个英文字母)的字符串或行
5.
findstr "[abcezy]" 2.txt
从文件2.txt中查找包括a b c e z y字母的字符串或行
6.
findstr "[a-fl-z]" 2.txt
从文件2.txt中查找小写字符a-f l-z的字符串,但不包含g h I j k这几个字母。
7.
findstr "M[abc][hig]Y" 2.txt
从文件2.txt中可以匹配 MahY , MbiY, MahY等…..
8.
^和$符号的应用
^ 表示行首,"^step"仅匹配 "step hello world"中的第一个单词
$ 表示行尾,"step$"仅匹配 "hello world step"中最后一个单词
9.
finstr "[^0-9]" 2.txt
任何不在字符集0-9中的字符,即存在字符不属于0-9就打印,
比如dfd41210 的对方的45 等 就会打印;而54545158将不被打印.
10.
findstr "[^a-z]" 2.txt
任何不在字符集a-z中的字符,即存在字符不属于a-z就打印.
比如adfdfd2225 dfdfdfdfd大富大贵 等将被打印;而dfdfdffd不被打印
11.
*号的作用
前面已经说过了 ".*"表示搜索的条件是任意字符,*号在正则表达式中的作用不是任何字符,而是表示左侧字符或者表达式的重复次数,*号表示重复的次数为零次或者多次。
12.
findstr "^[0-9]*$" 2.txt
这个是匹配找到的纯数字,^ 是代表开头 [0-9] 代表数字 * 代表重复0或多次 $ 代表结尾.
例如 234234234234,如果是2133234kkjl234就被过滤掉了。但是单个的数字不匹配.
Findstr "^[a-z]*$" 2.txt
这个是匹配找到的纯字母,例如 sdfsdfsdfsdf,如果是213sldjfkljsdlk就被过滤掉了
如果在搜索条件里没有*号,也就是说不重复左侧的搜索条件,也就是[0-9] [a-z]那只能匹配字符串的第一个字符也只有这一个字符,因为有行首和行尾的限制,"^[0-9]$"第一个字符如果是数字就匹配,如果不是就过滤掉,如果字符串是 9 就匹配,如果是98或者9j之类的就不可以了。
13.
"\<…\>"这个表达式的作用
这个表示精确查找一个字符串,\<sss 表示字的开始位置,sss\>表示字的结束位置
echo hello world computer|findstr "\<computer\>"这样的形式
echo hello worldcomputer|findstr "\<computer\>" 这样的形式就不成了,他要找的是 "computer"这个字符串,所以不可以。
echo hello worldcomputer|findstr ".*computer\>"这样就可以匹配了
14. "."表示任意字符.
findstr /rc:" 01...... " test.txt>>a1.txt
findstr /rc:" 02...... " test.txt>>a2.txt
15.
查找英文状态下的句号"."
在"."的前面加转义字符"\"
set abc=abc de.f
echo %abc%|findstr /c:"\."
16. 查找"\"
set abc=abc E:\123 1111
echo %abc%|findstr "E:\\"
17. /g用法
将1.txt的每行作为搜索字符串,在2.txt中搜索.(如果1.txt的某行含有空格也视为有效字符,不解释为"或")
findstr /g:1.txt 2.txt
加上/b/e 就是行首行尾完全匹配
findstr /b /e /g:1.t 2.t(或findstr /beg:1.t 2.t)
18. /o参数
((echo.%str%&echo. )|findstr /o .)|findstr /c:" "
范例:
findstr . test.txt
过滤空行打印
findstr .* test.txt
原样打印,这个不同与find /v "" 后者会给结果前加上 ---------- 目标文件全名
findstr . test.txt|findstr /v /r /c:"^ * $"
过滤空行,纯空格行打印
-------------
有一A.TXT文件,在其中找到JKLJHLL时,删除含有JKLJHLL字符串的行(不分大小写).
findstr /ivc:"JKLJHLL" a.txt >b.txt

