[英]Powershell V2 find and replace
我正在尝试以编程方式在文件中更改日期。 我需要修复的行如下所示:
set @@dateto = '03/15/12'
我需要编写一个Powershell V2脚本来替换单引号中的内容,而且我不知道该怎么做。
我最接近的是这样的:
gc $file | ? {$_ -match "set @@dateto ="} | % {$temp=$_.split("'");$temp[17]
=$CorrectedDate;$temp -join ","} | -outfile newfile.txt
问题:它给出了有关索引17超出范围的错误。 此外,输出文件仅包含一行(未修改的行)。 我将不胜感激。 谢谢!
您可以执行以下操作(尽管您可能想处理一些极端情况):
$CorrectedDate = '10/09/09'
gc $file | %{
if($_ -match "^set @@dateto = '(\d\d/\d\d/\d\d)'") {
$_ -replace $matches[1], $CorrectedDate;
}
else {
$_
}
} | out-file test2.txt
mv test2.txt $file -force
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.