If you’re familiar with grep and sed on Linux, you may come to miss them in a Windows environment (although the WSL subsystem / GNU tools for Windows exist, so…). Thankfully, with Powershell we have equally competent tools at hand.
Select-String -Pattern 'myRegexPattern' -Path 'fileToSearch.txt' -AllMatches | % { $_.Matches.Value -replace "old","new" }
Select-String
will take over the role of grep and spits out all matching strings. These are then piped into a ForEach
loop using the shorthand %
notation. Within the loop, $_
refers to the the current item. Since a match is an object and not just a string, it has a number of variables in it. The string is in the Value variable. -replace
works on the string and replaces old for new.
You can immediately continue to work with those string by e.g. joining them together with others, like so:
Select-String -Pattern 'myRegexPattern' -Path 'fileToSearch.txt' -AllMatches | % { $_.Matches.Value -replace "old","new" } | % {-join ($baseUrl, "/somefolder/", $_ ) }