end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

windows power shell での grep代替 Select-String の練習

https://end0tknr.hateblo.jp/entry/20231004/1696402476

先日の上記entryの続き?です

ファイル内の文字列を正規表現でキャプチャし、結果を別ファイルへ出力

Get-Content -Path "SVN_TEST/LockOwners.txt" | ForEach-Object {
    if ($_ -match "Owner:(.*)") {
        $Matches[1] | Out-File -Append -FilePath "SVN_TEST/LockOwners2.txt"
    }
}

ファイル内に更に記載されたPathのファイル内に windowsのログインユーザがある場合、出力

$filePath = "SVN_TEST/OwnersFiles.txt"
$innerFilePaths = Get-Content -Path $filePath

$results = foreach ($innerFilePath in $innerFilePaths) {
    Get-Content -Path $innerFilePath | Select-String -Pattern "^$env:USERNAME$"
}
$results

ちなみに、SVN_TEST/OwnersFiles.txt の内容は以下

SVN_TEST/LockOwners0.txt
SVN_TEST/LockOwners1.txt
SVN_TEST/LockOwners2.txt

更に、ちなみに、SVN_TEST/LockOwners0.txt の内容は以下

Owner:tendou
Owner:end0tknr