Windows下尋找純文字檔案中的字串 – 命令findstr簡介)

來源:互聯網
上載者:User
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 表示把給定的搜尋字串作為Regex來看待。預設情況下,輸出的結果內容的格式為:檔案名稱:目標字串所在行。如果要列印出行號,需要加上/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秒。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.