5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerShellのSelect-Stringで前後行を含む文字列だけを取得する

Last updated at Posted at 2022-03-21

はじめに

PowerShellのSelect-Stringでは、Contextパラメーターを使用すると、検索でヒットした行の「前後行を含めた結果」を取得することができます。
この前後行を含めて、該当行の「文字列のみ」を取得しようとしたところ、少し工夫が必要だったため、今回はその内容を記載します。

環境

  • Windows PowerShell 5.1.22000.282

結論

Select-String -Path [ファイル名] -Pattern [検索パターン] -Context [前の行数], [後の行数] | % { $_.Context.PreContext + $_.Line + $_.Context.PostContext }

コマンドの具体例は、成功例で確認可能です。

検証

やりたいこと

  • Select-Stringでヒットした行の、前後行を含めた文字列のみを取得する

検証用データ

検証用に、以下のようなファイルを準備しました。

sample.txt
◆あ行
・あ
・い
・う
・え
・お

◆か行
・か
・き
・く
・け
・こ

◆さ行
・さ
・し
・す
・せ
・そ

成功例

"◆"を含む行とその後ろ2行までを取得する場合のコマンドです。

Select-String -Path sample.txt -Pattern "◆" -Context 0, 2 | % { $_.Context.PreContext + $_.Line + $_.Context.PostContext }

結果

◆あ行
・あ
・い
◆か行
・か
・き
◆さ行
・さ
・し

該当行とその後ろ2行を含んだ文字列だけを取得できています。

失敗例

結果をそのまま取得

Select-String -Path sample.txt -Pattern "◆" -Context 0, 2

結果


> sample.txt:1:◆あ行
  sample.txt:2:・あ
  sample.txt:3:・い
> sample.txt:8:◆か行
  sample.txt:9:・か
  sample.txt:10:・き
> sample.txt:15:◆さ行
  sample.txt:16:・さ
  sample.txt:17:・し


該当行を示す>やファイル名なども含まれてしまいます。

Select-Objectで、LineContextを選択して取得

Select-String -Path sample.txt -Pattern "◆" -Context 0, 2 | Select-Object Line, Context

結果


Line Context                                       
---- -------                                       
◆あ行  Microsoft.PowerShell.Commands.MatchInfoContext
◆か行  Microsoft.PowerShell.Commands.MatchInfoContext
◆さ行  Microsoft.PowerShell.Commands.MatchInfoContext


該当行のみの情報が欲しい場合は、Select-ObjectLineで絞り込めばよかったのですが、前後行(Context)については、この方法ではうまくいきません。

調査

ContextプロパティをGet-Memberしてみたところ、PreContextプロパティに前の行、PostContextプロパティに後の行の結果が格納されているようでした。
以下は、Contextプロパティを表示させた例です。

PS D:\workspace> Select-String -Path sample.txt -Pattern "◆" -Context 0, 2 | % { $_.Context }

PreContext PostContext  DisplayPreContext DisplayPostContext
---------- -----------  ----------------- ------------------
{}         {・あ, ・い} {}                {・あ, ・い}
{}         {・か, ・き} {}                {・か, ・き}
{}         {・さ, ・し} {}                {・さ, ・し}

Microsoft Docsにも記載がありました。

Properties(一部抜粋)

PostContext Lines found after a match.
PreContext Lines found before a match.
引用元:MatchInfoContext Class (Microsoft.PowerShell.Commands) | Microsoft Docs

https://docs.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.matchinfocontext?view=powershellsdk-7.0.0

これをLineプロパティと結合させると、結論にあるコマンドとなり、意図した結果を取得できるようになりました。

参考

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?