文章目錄
findstr - Windows 上的 grep (快速搜尋文件)
寫程式難免會遇到暴力搜尋的情境,在史上最強開發 IDE - Visual Studio 的幫助下,一向不是什麼難事,但如果遇到想要找文件時怎麼辦,在 Linux 環境下有 grep 指令可以用,在 Windows 上一直都是使用保哥推薦的工具- grepWin, 保哥有簡單的教學-介紹好用工具:grepWin, 最近同事則是推薦 Windows 的 findstr 語法
語法
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:][Path] FileName [...]]
參數說明
/b: 搜尋字串在一行的開頭findstr /b npm \*.\*/e: 搜尋字串在一行的結尾(這個我測試起來,如果字串在檔案的結尾會找不到)findstr /e npm \*.\*/l: 將字串內的符號當作定字串來搜尋findstr /l "\"npm" t1.txtnpm 12354 "npm只有
"npm符合條件/r: 使用regular expression搜尋findstr /r "[^0-9]" t1.txt/s: 在目前目錄并包含子目錄搜尋findstr /s npm *.*/i: 忽略大小寫差異findstr /i npm *.*/x: 把完全符合的那行輸出(不得與/m共用,行需完全符合才會輸出)findstr /x npm *.*/v: 把不包含的那行內容輸出findstr /v npm *.*/n: 把符合條件的行數列出findstr /n npm *.*/m: 如果有找到符合的內容,僅列出檔案名稱findstr /m npm *.*/o: 印出該行內容前還有幾個字元findstr /o npm *.*/p: 忽略不可視字元findstr /p npm *.*/offline: 搜尋帶有offline屬性的檔案。offline 屬性是別台電腦的檔案留存在本機上的複本或是檔案擁有者是別台電腦. offline 屬性可以看 File attribute of “O” and unusual file icon overlay./f: file: 使用特定檔案所列的檔案清單來搜尋findstr /f:files.txt npm/c: string: 使依指定字串內容搜尋findstr /c:"npm install" \*.\*- 會將指定的內容視為一個整體,可以搜尋空白
/g: file: 搜尋字串由指定的檔案來findstr /g:t1.txt t2.txt- 將 t2.txt 中包含 t1.txt 任一行內容的那行內容輸出
/d: dirlist: 指定搜尋目錄清單,以;分割findstr /D:"C:\Notes\react";"C:\npm" npm *.*- 在
C:\Notes\react及C:\npm所有檔案中搜尋npm
- 在
/a: ColorAttribute: 指定輸出顏色(使用16進位顏色).findstr /a:2F npm *.txt- 有所有 txt 檔中搜尋
npm,檔名使用藍色輸出
- 有所有 txt 檔中搜尋
strings: 要搜尋的內容findstr npm \*.\*在所有檔案中搜尋
npm空格分割多個要搜尋的字串
[ Drive : ][ Path ] FileName [...]: 指定要搜尋的檔案位置findstr npm c:\t1.txt- 在
c:\t1.txt中搜尋npm
- 在
/?: 列出語法相關說明findstr /?
小技巧
語法參數可以串連
findstr /s /i /n npm \*.\*findstr /sin npm \*.\*- 上面兩句是相同的
語法參數跳脫字元可以用
/及-findstr /b npm \*.\*findstr -b npm \*.\*- 上面兩句是相同的
findstr 可以搜尋 office 97-2003 的檔案內容
- 新版 office 檔案(副檔名有
x)無法搜尋到 - grepwin 無法搜尋到 office 97-2003 的檔案內容
- 新版 office 檔案(副檔名有
參考資料
文章作者 Yowko Tsai
上次更新 2021-11-03
授權合約
本部落格 (Yowko's Notes) 所有的文章內容(包含圖片),任何轉載行為,必須通知並獲本部落格作者 (Yowko Tsai) 的同意始得轉載,且轉載皆須註明出處與作者。
Yowko's Notes 由 Yowko Tsai 製作,以創用CC 姓名標示-非商業性-相同方式分享 3.0 台灣 授權條款 釋出。
