mike85
mike85

Reputation: 13

Replace content in http link from a text file in powershell

I am new in powershell and I have created the following script which it extracts what's between http:// and the next /, transform it and then replace the intial match:

$fileName = "myfile"
$newEnvironment = "NewEnvironment"
$config = Get-Content $fileName
$newConfig = $config | % { $_ -replace "http://www.site.de", "http://site.de.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.com.tr", "http://site.com.tr.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.fr", "http://site.fr.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.pl", "http://site.pl.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site-1.be", "http://site-1.be.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site-1.nl", "http://site-1.nl.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.it", "http://site.it.$newEnvironment" }
$newConfig | Set-Content $fileName 

I'm trying to make it better, maybe using regex or something else but not using hard coded text. Could anyone please help me with this ?

I was thinking something like:

$path = "myFile";
Get-Content $path | Foreach {$_ -replace "(?<=http://).+?(?=/.*)",".+?(?=/.*).newEnvironment"};
Set-Content $path;  

But It did not work, even if it was setting the links in this way:

http://.+?(?=/.*).newEnvironment/asd/test.aspx

Upvotes: 1

Views: 98

Answers (1)

Tomalak
Tomalak

Reputation: 338326

It seems you want to

  • remove the "www." part
  • append the value of $newEnvironment to any URL

One way to do it would be to search for text that...

  • is preceded by "http://" — (?<=http://)
  • starts with "www." — www\.
  • has characters other than space or slash, as group 1 — ([^/ ]+)
  • for safety, is not already followed by $newEnvironment — (?!\.$newEnvironment)

and replace it with "regex group 1" + "." + $newEnvironment:

$fileName = "myfile"
$newEnvironment = "NewEnvironment"

$pattern = "(?<=http://)www\.([^/ ]+)(?!\.$newEnvironment)"
$replacement = "`$1.$newEnvironment"

(Get-Content $path) -replace $pattern,$replacement | Set-Content $path

Powershell operators are generally happy with arrays. Get-Content will give you an array of lines and -replace will work on all of them. (Another practical property of -replace is that you can chain it: "abc" -replace "a","A" -replace "b","B" will work.)

This means there is no need to write a manual foreach loop. The only thing required is a pair of parentheses so Get-Content does not mistake -replace for a parameter.

$1 is the back-reference to group 1, the backtick is PowerShell's escape character, because $ itself has meaning in both Powershell and regex.

Upvotes: 2

Related Questions