1

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
2
  • Worked for me without the space after the $ in the replace. "Embedded 6.0 (2010)" | % { $_ -replace "^Embedded 6.0.*$", "Embedded 6.0 (XXXX)" } Commented Dec 8, 2016 at 20:32
  • I needed to modify my question, it wasn't exactly accurate Commented Dec 8, 2016 at 21:05

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

ahhh i wasn't clear in my question the 2010 in the string is the variable and will not be the same. However, i just copied and paste your solution and it did not modify, if anything it should replace that line...i wonder where i went wrong
So what are you searching for? Four digits will be represented by \d{4}
another thing since there are mutiple entries of 6.0.0.0 in my text file, how do i constrained this match '6.0.0.0.\d{4}' to have to be followed by 'Wave Embedded'
There was still a flaw, the parentheses in the search have to be escaped because they have a special meaning in a RegEx (capture group). I edited my script, It should work now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.