このスクリプトはうまく機能しますが、行番号と行を返すためにも必要です。
私が行った場合
Select-String w:\test\york\*.* -pattern "mistake"
私は得る
W:\test\york\test.html:179:<p>情報伝票の誤りに気付いた場合は、その伝票について雇用主、支払人、または管理者に連絡してください。</p> W:\test\york\test.html:180:<p>税金関連の情報の誤りに気づいた場合、またはその他の税金関連の情報についてアカウント固有の質問がある場合は、個人所得税と信託の問い合わせラインに電話してください。 1-800-959-8281.</p>
これは完璧です。ただし、私のスクリプトでは、これは次のとおりです。
param(
[string]$pattern,
[string]$path
)
$exclude = '*\test\*'
Get-ChildItem -Path $path -Recurse -Filter *.html | Where-Object {
ForEach-Object {
if (Get-Content $_.FullName | Select-String -Pattern "<h2>Stay Connected") {
Select-String -InputObject (Get-Content $_.FullName | Out-String) -Pattern "(?sm)<main([\w\W]*)$pattern([\w\W]*)<h2>Stay Connected"
} elseif (Get-Content $_.FullName | Select-String -Pattern "<h2>Soyez branch") {
Select-String -InputObject (Get-Content $_.FullName | Out-String) -Pattern "(?sm)<main([\w\W]*)$pattern([\w\W]*)<h2>Soyez branch"
} else {
Select-String -InputObject (Get-Content $_.FullName | Out-String) -Pattern "(?sm)<main([\w\W]*)$pattern([\w\W]*)<\/main>"
}
}
} | Select Fullname | ? {$_.FullName -notlike $exclude}
CSVの結果としてこれを取得するだけです:
#TYPE Selected.System.IO.FileInfo フルネーム W:\test\york\test.html
スクリプトを使用して、単純な検索の結果を CSV ファイルで取得するにはどうすればよいですか?
Ansgar Wiechers の回答に従って編集されました。
$pattern
「教科書」です。
これは私の現在のコードです:
param(
[string]$pattern,
[string]$path,
[string]$name
)
$expr = "(?sm)<main([\w\W]*)$pattern([\w\W]*)" +
'(?:<h2>Stay Connected|<h2>Soyez branch|<\/main>)'
Get-ChildItem -Path $path -Recurse -Filter *.html |
Select-String -Pattern $expr |
Select-Object Path, LineNumber, Line |
Export-Csv "W:\test\search_results\$name.csv" -NoType
交換したら
$expr = "(?sm)<main([\w\W]*)$pattern([\w\W]*)" +
'(?:<h2>Stay Connected|<h2>Soyez branch|<\/main>)'
に
$expr = $pattern
6 つの結果 (正しい) が得られますが、Ansgar によって提供された式を使用すると、結果が得られません。式を適切に機能させるには何が欠けていますか?