Select-Stringでテキスト検索検索の指定方法ファイルのテキスト検索Select-String -Path "*.txt" -Pattern "hello" Patternで指定する文字列はデフォルトで正規表現が使える。 エイリアス・PathとPatternの省略sls "hello" *.txt OR検索sls "hello", "world" *.txt "hello"または"world"がある行にマッチする。 NOT検索sls -Path *.txt -NotMatch -Pattern "hello","world" "hello"も"world"もない行にマッチする。 AND検索sls "hello" *.txt | sls "world" または Get-Content *.txt | Where-Object { $_ -match "hello" -and $_ -match "world" } "hello"と"world"がある行にマッチする。 -AllMatchesオプション 各行で複数の一致を検索"hello, hello"と書かれたtest.txtがあるとする。 PS> gc test.txt hello, hello AllMachesオプションなしでは、 PS> $ret = sls "hello" test.txt PS> $ret.Matches Groups : {hello} Success : True Captures : {hello} Index : 0 Length : 5 Value : hello 1文字目(Index:0)のhelloだけキャプチャーされる。AllMachesオプションありでは PS> $ret = sls "hello" test.txt -AllMatches Groups : {hello} Success : True Captures : {hello} Index : 0 Length : 5 Value : hello Groups : {hello} Success : True Captures : {hello} Index : 6 Length : 5 Value : hello 1文字目に加えて、5文字目(Index:6)のhelloもキャプチャーされる。 検索結果の加工マッチしたファイル名を取得sls "hello" *.txt | Select-Object filename | Get-Unique -AsString
参考 |
|