Switch strings in a file

后端 未结 2 709
春和景丽
春和景丽 2020-11-30 14:29

I have a string needs to be changed in a file between two values. What I want to do is if I found value A then change to value B, if I found value B then change to value A.

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 15:15

    This isn't tested extensively, but I think it should work:

    path = c:\work\test.xml
    $A = 'AAAAA'
    $B = 'BBBBB'
    
    [regex]$regex = "$A|$B"
    
    $text = 
    Get-Content $path | 
    foreach {
    $regex.Replace($text,{if ($args[0].value -eq $A){$B} else {$A}})
    }
    
    $text | Set-Content $path
    

    Hard to be sure without knowing exactly what the data looks like.

提交回复
热议问题