Windows中的find和findstr命令有什么区别?


24

在Windows中,findfindstr命令之间有什么区别?

两者似乎都在文件中搜索文本:

C:\> 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.

findstr

C:\> findstr /?
Searches for strings in files.

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 occurences 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.

1
它可能更多地与进化有关。查找可以追溯到DOS / UNIX时代,后来在Windows中添加了FINDSTR。两者都有可能发展,并且变得越来越相似。
KCotreau

我同意@KCotreau的时间表:FIND是古老的,而FINDSTR是更新的。我怀疑FIND已经演变为更像FINDSTR了;相反,我相信FIND被保留为残缺的恐龙(通常与DOS一样),以保持向后兼容性(例如,使用它的批处理文件),而FINDSTR被添加以提供一组不错的功能。
斯科特,

1
哦,顺便说一句,DOS / Windows FIND命令与Unix find命令完全不同。相反,正如下面的悖论所暗示的那样,FIND就像grep(或fgrep)的淡化版本。
斯科特,

您没有看到帮助中的区别吗?
phuclv

Answers:


17

如上所示,findstr增加了对正则表达式的支持,因此更像grep


3
但是,如果您不经常使用这两种方法,那么总是不清楚确切的用法和区别。因此,他们的看法可能会有所不同,因为您可能经历了与他们不同的经历。在学习和职业中,一个巨大问题的一部分就是“体验差距”。很长的故事。
crosenblum

5

Findstr具有更多搜索选项并支持正则表达式。我发现findstr不适用于文件名中的通配符。

以下命令以Quant_2013-10-25 _ *。log模式返回多个文件中所有搜索字符串的出现

find /I "nFCT255c9A" D:\Comp1\Logs\Quant_2013-10-25_*.log 

以下命令不返回任何内容或根本不起作用

findstr nFCT255c9A D:\Comp1\Logs\Quantum_2013-10-25_*.log

find也有一些限制。例如,您不能使用它在每行后面附加行号,因为find/n""不会匹配任何内容。为此,您必须使用findstr
Pacerier,2015年

1
无法复制中的通配符问题findstrMicrosoft Windows 7 Professional, 6.1.7601 Service Pack 1 Build 7601
lordcheeto 2015年


1

findstr通过一些有用的功能扩展了find的功能。一些关键的补充包括

  1. findstr支持多个搜索字符串
  2. findstr可以将包含要搜索的文件名或目录的文件作为输入
  3. findstr支持正则表达式

两种功能均不适用于大文件或大量文件。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.