DOS命令FINDSTR

Windows 官方网站:各种命令详解。
http://technet.microsoft.com/en-us/library/bb490907.aspx

在文件中寻找字符串。
  FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/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\> 字位置: 字的结束

FINDSTR正则表达式的基本用法

1.findstr . 2.txt 或 Findstr "." 2.txt 
从文件2.txt中查找任意字符,不包括空字符或空行
====================

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中查找包括任意字符的字符串或行
====================

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
如果是纯数字的字符串或者行便过滤掉,例如2323423423 这样的字符串,如果是345hh888这样的形式就不成了。
====================

10.findstr "[^a-z]" 2.txt
同上,如果是纯字母的字符串或者行便过滤掉,例如 sdlfjlkjlksjdklfjlskdf这样的字符,如果是sdfksjdkf99999这样的形式,掺杂着数字就不成了
====================

11.*号的作用
前面已经说过了 ".*"表示搜索的条件是任意字符,*号在正则表达式中的作用不是任何字符,而是表示左侧字符或者表达式的重复次数,*号表示重复的次数为零次或者多次。
====================

12.findstr "^[0-9]*$" 2.txt
这个是匹配找到的纯数字,例如 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\>"这样就可以匹配了
=====================


Syntax

findstr [/b] [/e] [/l] [/r] [/s] [/i] [/x] [/v] [/n] [/m] [/o] [/p] [/offline] [/g:file] [/f:file] [/c:string] [/d:dirlist] [/a:ColorAttribute] [strings] [[Drive:][PathFileName [...]]

Parameters

/b   : Matches the pattern if at the beginning of a line.

/e   : Matches the pattern if at the end of a line.

/l   : Uses search strings literally.

/r   : Uses search strings as regular expressions. Findstr interprets all metacharacters as regular expressions unless you use /l.

/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 file name if a file contains a match.

/o   : Prints seek offset before each matching line.

/p   : Skips files with non-printable characters.

/offline   : Processes files with offline attribute set.

/f: file   : Reads file list from the specified file.

/c: string   : Uses specified text as a literal search string.

/g: file   : Gets search strings from the specified file.

/d: dirlist   : Searches a comma-delimited list of directories.

/a: ColorAttribute   : Specifies color attributes with two hexadecimal digits.

strings   : Specified text to be searched for in FileName.

Drive : ][ Path ] FileName [...] : Specifies a file or files to search.

/?   : Displays help at the command prompt.

Remarks

  • Using regular expressions with findstr 

    Findstr is capable of finding the exact text you are looking for in any ASCII file or files. However, sometimes you have only part of the information that you want to match, or you want to find a wider range of information. In such cases, findstr has the powerful capability to search for patterns of text using regular expressions.

    Regular expressions are a notation for specifying patterns of text, as opposed to exact strings of characters. The notation uses literal characters and metacharacters. Every character that does not have special meaning in the regular expression syntax is a literal character and matches an occurrence of that character. For example, letters and numbers are literal characters. A metacharacter is a symbol with special meaning (an operator or delimiter) in the regular-expression syntax.

    The following table lists the metacharacters that findstr accepts.

    Character

    Value

    .

    Wildcard: any character

    *

    Repeat: zero or more occurrences 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

    The special characters in regular expression syntax are most powerful when you use them together. For example, the following combination of the wildcard character (.) and repeat (*) character match any string of characters:

    .*

    Use the following expression as part of a larger expression that matches any string beginning with "b" and ending with "ing":

    b.*ing

Examples

Use spaces to separate multiple search strings unless the argument is prefixed with /c. To search for "hello" or "there" in file x.y, type:

findstr "hello there" x.y

To search for "hello there" in file x.y, type:

findstr /c:"hello there" x.y

To find all occurrences of the word "Windows" (with an initial capital W) in the file Proposal.txt, type the following:

findstr Windows proposal.txt

To search every file in the current directory and all subdirectories that contained the word Windows, regardless of the letter case, type the following:

findstr /s /i Windows *.*

To find all occurrences of lines that contain the word "FOR", preceded by any number of spaces, (as in a computer program loop), and to include the line number where each occurrence is found, type the following:

findstr /b /n /c:" *FOR" *.bas

If you want to search for several different items in the same set of files, create a text file that contains each search criterion on a new line. You can also list the exact files you want to search in a text file. To use the search criteria in the file Finddata.txt, search the files listed in Filelist.txt, and then store the results in the file Results.out, type the following:

findstr /g:finddata.txt /f:filelist.txt > results.out

Assume you wanted to find every file in the current directory and all subdirectories that contained the word computer, regardless of the letter case. To list every file containing the word computer, type the following:

findstr /s /i /m "\<computer\>" *.*

Now assume you want to find not only the word "computer," but also any other words that begin with the letters comp, such as "compliment" and "compete. " type the following:

findstr /s /i /m "\<comp.*" *.*

Formatting legend

Format

Meaning

Italic

Information that the user must supply

Bold

Elements that the user must type exactly as shown

Ellipsis (...)

Parameter that can be repeated several times in a command line

Between brackets ([])

Optional items

Between braces ({}); choices separated by pipe (|). Example: {even|odd}

Set of choices from which the user must choose only one

Courier font

Code or program output



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值