1

私はpowershellのselect-stringを使用していますが、うまく機能しますが、(私にとっては)不要な情報が含まれているため、一致した行全体が表示されるのが気に入らない場合があります。

正規表現の前に10文字、正規表現と15文字後に与える簡単な方法はありますか? 私が今それをどのように使用するかの例:

gci *.log | select-string "lastname"
4

3 に答える 3

3

正規表現を作成し、代わりに演算子(.{10})lastname(.{15})を使用Where-Objectします。-matchSelect-String

gci *.log | ? { $_ -match '(.{10})lastname(.{15})' } | % {
  "{0}`t{1}" -f ($matches[1], $matches[2])
}
于 2013-06-12T15:07:01.530 に答える
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
于 2013-06-12T17:57:33.960 に答える
2

グループを使用する必要があります。次のように名前付きグループを使用できます。

gci c:\temp\_sotemp\*.log | 
    select-string -Pattern "(?<refvalue>lastname)" | 
    select -expand Matches | 
    foreach {$_.groups["refvalue"].value}
于 2013-06-12T15:06:21.547 に答える