findstr command in Windows CMD

1. Introduction to findstr command

findstr is an extension of find with more powerful functions. The key is that it still supports regular expressions.

View the help information of the findstr command

C:\Users\DELL>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         在一行的开始配对模式。
  /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 常见表达法的详细情况,请见联机命令参考。

C:\Users\DELL>

grammatical format

Format: findstr [optional parameter] string to find [path\filename]

important point:

  1. The default is to search the current directory , specify at least one file, of course, you can specify multiple at the same time, separated by spaces.
  2. File names can use wildcards, for example, all text files can be written as *.txt.

Two, examples

1. Basic use

findstr "abc" d:\test.txt

Similar to the find command.  Indicates to find all the lines containing the string abc in the test.txt file under the D disk  .

2. Find strings with spaces

findstr  /c:"abc xyz" d:\test.txt

It means to find the lines containing "abc xyz" (note that there is a space in the middle), if the parameter /c is not used: findstr "abc xyz" d:\test.txt will output the lines containing the letters abc or xyz.

3. Search in all files in the current directory and all subdirectories

D:\test>findstr /s /i "abc"  *.*

Search for the string "abc" in all files in the current directory (D:\test) and all subdirectories , *.* means all types of files, and does not distinguish the case of the string.

4. The use of regular expressions

Lines that match pure letters are written as follows:

D:\>findstr "^[a-z]*$" test.txt

Match the lines beginning with abc, written as follows:

D:\>findstr "^abc" test.txt

おすすめ

転載: blog.csdn.net/icanlove/article/details/37567253