Muni Bus

パソコンの操作方法や設定方法を忘れないようにメモしています。ブログを一回引っ越ししているので、所々表示がかなり乱れています・・・

【PowerShell】ファイルから指定の文字列が含まれる行を抜き出す

Select-Stringコマンドレットを使う。デフォルトでは大文字小文字の違いを無視するため、大文字小文字を区別する場合は-CaseSensitiveオプションを付ける。指定の文字列には正規表現が使える。ファイル名と行番号が表示されるのは仕様で、これを外したい場合は、Get-Contentコマンドレットの結果をパイプで渡す。最後の例は、それぞれエイリアスを使っている。以下の例では、空行は適宜省略している。

PS > Get-Content C:\Windows\system.ini
; for 16-bit app support
[386Enh]
woafont=dosapp.fon
EGA80WOA.FON=EGA80WOA.FON
EGA40WOA.FON=EGA40WOA.FON
CGA80WOA.FON=CGA80WOA.FON
CGA40WOA.FON=CGA40WOA.FON
[drivers]
wave=mmdrv.dll
timer=timer.drv
[mci]
PS > Select-String -Pattern "FON" -Path C:\Windows\system.ini
C:\Windows\system.ini:3:woafont=dosapp.fon
C:\Windows\system.ini:4:EGA80WOA.FON=EGA80WOA.FON
C:\Windows\system.ini:5:EGA40WOA.FON=EGA40WOA.FON
C:\Windows\system.ini:6:CGA80WOA.FON=CGA80WOA.FON
C:\Windows\system.ini:7:CGA40WOA.FON=CGA40WOA.FON
PS > Select-String -Pattern "FON" -Path C:\Windows\system.ini -CaseSensitive
C:\Windows\system.ini:4:EGA80WOA.FON=EGA80WOA.FON
C:\Windows\system.ini:5:EGA40WOA.FON=EGA40WOA.FON
C:\Windows\system.ini:6:CGA80WOA.FON=CGA80WOA.FON
C:\Windows\system.ini:7:CGA40WOA.FON=CGA40WOA.FON
PS > Select-String -Pattern "[4-6]0" -Path C:\Windows\system.ini -CaseSensitive
C:\Windows\system.ini:5:EGA40WOA.FON=EGA40WOA.FON
C:\Windows\system.ini:7:CGA40WOA.FON=CGA40WOA.FON
PS > cat C:\Windows\system.ini | sls "[7-8]0"
EGA80WOA.FON=EGA80WOA.FON
CGA80WOA.FON=CGA80WOA.FON