1

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 
2
  • So replace id= and any characters that follow that until the end of the line? (Get-Content C:\test.txt) | ForEach {$_ -replace "id=.*","id=1234"} Commented Jan 10, 2019 at 13:16
  • A variant with a positive lookbehind (gc C:\test.txt) -replace '(?<=id=)[^\s]+','1234' Commented Jan 10, 2019 at 20:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)
)

Comments

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.