findstrコマンドで正規表現を用いた文字列検索をすることが可能です。
ただし、findstrの正規表現ですが、基本的なものだけとなっています。
以下にfindstrを使用した例をいくつか記します。
以下にfindstrを使った例をいくつか記します。
dirで出力されたファイル一覧からDIRを文字列を表示しています。
dir | findstr 検索文字列
c:\Users\sakura>dir | findstr DIR 2012/09/25 17:04 <DIR> . 2012/09/25 17:04 <DIR> .. 2012/09/12 15:06 <DIR> .thumbnails 2012/09/12 14:33 <DIR> Contacts 2012/09/27 16:39 <DIR> Desktop 2012/09/25 17:49 <DIR> Documents 2012/09/27 15:53 <DIR> Downloads 2012/09/12 14:33 <DIR> Favorites <snip>
Windowsディレクトリにあるlogファイルを対象にie9を検索した結果です。
findstr 検索文字列 対象ファイル
c:\Windows>findstr ie9 *.log WindowsUpdate.log:2012-09-25 16:59:54:518 980 e8c DtaStor WARNING: Attempted to add URL http://download.windowsupdate.com/msdownload/update/softwa re/secu/2012/09/ie9-windows6.1-kb2744842-x64_5d795303eee8eeb1d0f927edea10102ec59 4f9c2.msu for file XXlTA+7o7rHQ+Sft6hAQLsWU+cI= when file has not been previousl y added to the datastore WindowsUpdate.log:2012-09-25 17:00:07:241 980 e8c Handler Adding i e9-windows6.1-kb2744842-x64.cab (entire file) to request list. WindowsUpdate.log:2012-09-25 17:00:10:225 980 e8c DnldMgr * Down <snip>この例では、*を使用し拡張子がlogであるファイル全てを対象として検索しています。
検索文字列の間にスペースが含まれる場合は、/C:オプションを使用します。
findstr /C:"スペースを含む検索文字列" *.log
c:\Windows>findstr /C:"Windows 7 operating system" *.log IE9_main.log:00:04.625: INFO: Windows 7 operating system detected.
いくつかの検索方法を以下に記します。
C:\Users\sakura\Desktop>type text.txt abcdef1 defabc1 abcdef2 defabc2 abcdef3 defabc3実行結果
C:\Users\sakura\Desktop>findstr /B abc text.txt abcdef1 abcdef2 abcdef3
findstr /R "^検索文字列" ファイル名実行結果
C:\Users\sakura\Desktop>findstr /r "^abc" text.txt abcdef1 abcdef2 abcdef3
findstr /R /C:"^スペースを含む検索文字列" ファイル名検索するファイル内容
C:\Users\sakura\Desktop>type text2.txt a bcdef1 defa bc1 a bcdef2 defa bc2 a bcdef3 defa bc3実行結果
C:\Users\sakura\Desktop>findstr /r /c:"^a bc" text2.txt a bcdef1
いくつかの検索方法を以下に記します。
findstr /e 検索文字列 ファイル名検索するファイル内容
C:\Users\sakura\Desktop>cat text3.txt abc2def1 def2abc1 abc3def2 def3abc2 abc1def3 def1abc3
実行結果
検索文字列が行末にある行が表示されます。
C:\Users\sakura\Desktop>findstr /e 3 text.txt abcdef3 defabc3
C:\Users\sakura\Desktop>findstr /e c3 text.txt defabc3
実行結果
C:\Users\sakura\Desktop>findstr /r "3$" text.txt abcdef3 defabc3
オプション/Rを使用した正規表現検索の例をいくつか紹介します。
C:\Users\sakura\Desktop>cat text4.txt windows1 windows2 windows3 windows4 windows5 windows6 windows7 windows8 windows9実行結果
C:\Users\sakura\Desktop>findstr /r "windows[3-5]" text4.txt windows3 windows4 windows5
C:\Users\sakura\Desktop>findstr /r "windows[^3-7]" text4.txt windows1 windows2 windows8 windows9
オプションNを使用することにより検索された行の行番号が表示されます。
実行例
C:\Users\sakura\Desktop>findstr /n /r "windows[^3-7]" text4.txt 1:windows1 2:windows2 8:windows8 9:windows9
オプションIを使用することにより大文字・小文字を区別しないようになります。
実行例
C:\Users\sakura\Desktop>findstr windows text4.txt windows1 windows2 windows3 windows4 windows5 windows6 windows7 windows8 windows9 C:\Users\sakura\Desktop>findstr Windows text4.txt C:\Users\sakura\Desktop>findstr /I WiNdoWs text4.txt windows1 windows2 windows3 windows4 windows5 windows6 windows7 windows8 windows9
上記では、findstrの使いかたの一部を紹介しました。
findstr /?で使用方法が表示されますので、詳細な検索方法を知りたい場合は、findstr /?を実行してヘルプをみてみてください。