user5192051
user5192051

Reputation: 39

PowerShell script to search and replace text

Hello I have a script that searches for a matching string, then replace it

I want to replace all occurrence of "6.0.0.0.2010 Wave Embedded 6.0 (2010)" with "6.0.0.0.XXXX Wave Embedded 6.0 (XXXX)"

I want to match strings that match exactly that, however if i modify the 6.0.0.0.XXXX then it deletes everything after that..

my script :

(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

Upvotes: 1

Views: 632

Answers (1)

user6811411
user6811411

Reputation:

I had no problem with my teststring, is the occurence always the same?

"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)'}

So this should work also (you may insert a new line after a pipe or opening curly bracket):

$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

Upvotes: 3

Related Questions