您好我有一个搜索匹配字符串的脚本,然后替换它
我想将所有出现的“6.0.0.0.2010 Wave Embedded 6.0(2010)”替换为“6.0.0.0.XXXX Wave Embedded 6.0(XXXX)”
我想匹配与之完全匹配的字符串,但是如果我修改6.0.0.0.XXXX,那么它会在之后删除所有内容..
我的剧本:
(Get-Content C:\Users\gadmin\Desktop\temp\test.txt) | ForEach-Object { $_ -replace '6.0.0.0.*$', '6.0.0.0.XXXX' } | Set-Content C:\Users\gadmin\Desktop\temp\test1.txt
答案 0 :(得分:2)
我的测试字符串没问题,出现的情况总是一样吗?
"6.0.0.0.2010 Wave Embedded 6.0 (2010)"| ForEach {
$_ -replace '6.0.0.0.2010 Wave Embedded 6.0 (2010)',
'6.0.0.0.XXXX Wave Embedded 6.0 (XXXX)'}
所以这也应该有效(你可以在管道或开口花括号后插入一个新行):
$FileIn = "C:\Users\gadmin\Desktop\temp\test.txt"
$FileOut= "C:\Users\gadmin\Desktop\temp\test1.txt"
[RegEx]$Search = '6.0.0.0.\d{4} Wave Embedded 6.0 \(\d{4}\)'
Get-Content $FileIn -Raw| ForEach {
$_ -replace $Search, '6.0.0.0.XXXX Wave Embedded 6.0 (XXXX)'}|
Set-Content $FileOut