私はpowershellのselect-stringを使用していますが、うまく機能しますが、(私にとっては)不要な情報が含まれているため、一致した行全体が表示されるのが気に入らない場合があります。
正規表現の前に10文字、正規表現と15文字後に与える簡単な方法はありますか? 私が今それをどのように使用するかの例:
gci *.log | select-string "lastname"
私はpowershellのselect-stringを使用していますが、うまく機能しますが、(私にとっては)不要な情報が含まれているため、一致した行全体が表示されるのが気に入らない場合があります。
正規表現の前に10文字、正規表現と15文字後に与える簡単な方法はありますか? 私が今それをどのように使用するかの例:
gci *.log | select-string "lastname"
正規表現を作成し、代わりに演算子(.{10})lastname(.{15})
を使用Where-Object
します。-match
Select-String
gci *.log | ? { $_ -match '(.{10})lastname(.{15})' } | % {
"{0}`t{1}" -f ($matches[1], $matches[2])
}
Get-Member
プロパティを表示するには、常にパイプ オブジェクトを取得することを忘れないでください。
PS C:\> $text = 'asdfqwerasdfqwerlastnameasdfqwerasdfqwerasdf'
PS C:\> $text | Select-String -Pattern 'lastname' | Get-Member
Name MemberType Definition
---- ---------- ----------
...
Matches Property System.Text.RegularExpressions.Match[] Matches {get;set;}
PS C:\> ($text | Select-String -Pattern 'lastname').Matches
Groups : {lastname}
Success : True
Captures : {lastname}
Index : 16
Length : 8
Value : lastname
PS C:\> $index = ($text | Select-String -Pattern 'lastname').Matches.Index
PS C:\> $text[($index-10)..($index+15)] -join ''
erasdfqwerlastnameasdfqwer
グループを使用する必要があります。次のように名前付きグループを使用できます。
gci c:\temp\_sotemp\*.log |
select-string -Pattern "(?<refvalue>lastname)" |
select -expand Matches |
foreach {$_.groups["refvalue"].value}