现在的位置: 首页 > 综合 > 正文

Windows下查找纯文本文件中的字符串 – 命令findstr简介(转)

2013年01月30日 ⁄ 综合 ⁄ 共 4262字 ⁄ 字号 评论关闭

Windows下查找纯文本文件中的字符串 - 命令findstr简介

很多情况下,大家都有这样的感受:Windows资源管理器中的搜索功能形同虚设,根本起不到搜索的作用。这里,我给大家推荐一个相当给力的命令findstr - 强大的功能足以满足你搜索纯文本文件里的任何内容,并且打印出字符串所在文件,所在行,甚至行号。下面是findstr的使用说明和一些使用的实际例子: 

findstr使用说明 

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

For full information on FINDSTR regular expressions refer to the online Command 
Reference. 

findstr 使用示例: 
Example 1: 在Tomcat的logs目录下查找包含Catalina的所有log文件 
findstr /R ".*Catalina.*" * 
localhost_admin_log.2009-03-18.txt:2009-03-18 12:30:08 StandardContext[/admin]action: Tree expand/contract on Catalina:t 
ype=Service,serviceName=Catalina 
localhost_admin_log.2009-03-18.txt:2009-03-18 12:30:08 StandardContext[/admin]action: Found Node: Catalina:type=Service, 
serviceName=Catalina 
localhost_admin_log.2009-03-18.txt:2009-03-18 12:30:10 StandardContext[/admin]action: Tree expand/contract on Catalina:t 
ype=Host,host=localhost 

/R 表示把给定的搜索字符串作为正则表达式来看待。默认情况下,输出的结果内容的格式为:文件名:目标字符串所在行。如果要打印出行号,需要加上/N 选项: 
findstr /R/N ".*Catalina.*" * 
localhost_admin_log.2009-03-18.txt:2:2009-03-18 12:30:08 StandardContext[/admin]action: Tree expand/contract on Catalina 
:type=Service,serviceName=Catalina 
localhost_admin_log.2009-03-18.txt:3:2009-03-18 12:30:08 StandardContext[/admin]action: Found Node: Catalina:type=Servic 
e,serviceName=Catalina 
localhost_admin_log.2009-03-18.txt:5:2009-03-18 12:30:10 StandardContext[/admin]action: Tree expand/contract on Catalina 
:type=Host,host=localhost 

如果想把搜索的结果存入到文件中,使用导向符号>,后面跟上文件名: 
findstr /R/N ".*Catalina.*" * > result.txt 
使用导向符>会覆盖文件中旧的内容;如果不想覆盖旧的内容,使用导向符>> 

Example 2: 在Tomcat的logs目录下 查找以2009-03-18
12开头的所有log内容
 
findstr /N /C:"2009-03-18 12" * 
localhost_admin_log.2009-03-18.txt:9:2009-03-18 12:30:13 StandardContext[/admin]action: Found Node: Catalina:j2eeType=We 
bModule,name=//localhost/jforum-2.1.8,J2EEApplication=none,J2EEServer=none 
localhost_admin_log.2009-03-18.txt:10:2009-03-18 12:30:16 StandardContext[/admin]action: Entered TreeControlTestAction:p 
erform() 
localhost_admin_log.2009-03-18.txt:11:2009-03-18 12:30:16 StandardContext[/admin]action: tree param is null 

这里,我们用/C:"2009-03-18 12"指定所查找的字符串。因为,如果不用/C:的话,findstr 会用空格来分隔字符串,然后所有包含用空格分隔开的子串的文件。也就是说,默认情况下,空格起OR的作用。 

Example 3: 在Tomcat的logs目录下 查找不包含StandardContext的所有log内容,并且将结果log文件名和行号显示为蓝色。 
findstr /N/V/A:09 "StandardContext"  * 
  
选项 /V 表示在结果中显示不包含指定字符串的行及所在文件;选项 /A:attr 可以控制显示结果的颜色。 

findstr 的其他用法,这里就不一一列举了。值得一提的是 findstr 的运行速度很快,即使是搜索几十兆的大文件也不在话下。从一个139M的文件中,查找位于文件末尾的字符串所需的时间大概为18秒。 

抱歉!评论已关闭.