Stef_MOE
Stef_MOE

Reputation: 15

Replace multiple strings in a file with Powershell

We want to replace multiple instances of the variable id=*, with a certain pattern, like id=1234. I already made this Powershell script (and prefer to keep using Powershell as solution):

$line = Get-Content C:\test.txt | Select-String "id=" | Select-Object -ExpandProperty Line
$content = Get-Content C:\test.txt
$content | ForEach {$_ -replace $line,"id=1234"} | Set-Content C:\test.txt
Get-Content C:\test.txt

This works, as long as there's only 1 instance of id=..., when a file contains multiple instances of id=... there is no replacement step performed at all.

The input file is similar to:

text over here
id=1
text over here: id={123456}
text
id=number1
id=#3 text 
id=3+3 text 

which should result in:

text over here
id=1234
text over here: id=1234
text
id=1234
id=1234 text 
id=1234 text 

Upvotes: 1

Views: 4447

Answers (2)

Sage Pourpre
Sage Pourpre

Reputation: 10333

What you want is to capture every characters after id= until you hit a whitespace.

The following will work just fine

$content = Get-Content "C:test.txt" -raw
$content = $content -replace 'id=[^\s]*','id=1234'
Set-Content C:\test.txt
Get-Content C:\test.txt

Using the -Raw parameter will load the file quickly into a string instead of an array. From there, using the replace above, you will get the desired result.

The [^\s]* is to match a single character NOT including a character of whitespace (space, tab, carriage return, line feed)

You can use RegexStorm when creating regex statements.

See the regex I provided tested on there.

Upvotes: 1

Theo
Theo

Reputation: 61253

I think this will do:

Read the text as string array and replace line-by-line:

(Get-Content 'C:\test.txt') | 
    ForEach-Object { $_ -replace '(id\s*=\s*[^\s]+)', 'id=1234' } | 
    Add-Content -Path 'C:\test_updated.txt'

or read the text in as a single string and perform a Multiline replace ((?m))

(Get-Content C:\test.txt -Raw) -replace '(?m)(id\s*=\s*[^\s]+)', 'id=1234' | 
    Set-Content -Path 'C:\test_updated.txt'

I strongly recommend using a new filename for the output file so you do not overwrite the original.

In both cases, the code returns:

text over here
id=1234
text over here: id=1234
text
id=1234
id=1234 text 
id=1234 text

Regex Details

(            Match the regular expression below and capture its match into backreference number 1
   id        Match the characters “id” literally
   \s        Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
      *      Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   =         Match the character “=” literally
   \s        Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
      *      Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   [^\s]     Match any character that is NOT a “A whitespace character (spaces, tabs, line breaks, etc.)”
      +      Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)

Upvotes: 0

Related Questions