PowerShellスクリプトでは、ファイル内の文字列の最初の文字列を置き換えるために、置換が行われたかどうかを変数に記録します。ファイル内の最初の文字列を置き換えます。
もっとエレガントで(慣用的な)方法がありますか?
$original_file = 'pom.xml'
$destination_file = 'pom.xml.new'
$done = $false
(Get-Content $original_file) | Foreach-Object {
$done
if ($done) {
$_
} else {
$result = $_ -replace '<version>6.1.26.p1</version>', '<version>6.1.26.p1-SNAPSHOT</version>'
if ($result -ne $_) {
$done = $true
}
$result
}
} | Set-Content $destination_file
私はそれがXMLであることに気付かなかった。その場合、間違いなくこのようにします。 – EBGreen