grep and sed equivalent in PowerShell

Viewed 42467

I am trying to do the following statement in PowerShell

svn info filename | grep '^Last Changed Date:'| sed -e 's/^Last Changed Date: //'

I have tried this:

svn info filename | Select-String '^Last Changed Date:'

I am expecting below output

Thursday, December 20, 2018 4:50:40 AM
3 Answers

Use the -match and -replace operators:

(svn info filename) -match '^Last Changed Date:' -replace '^Last Changed Date: '

To remove the leading label you could use a capture group with the RegEx pattern.

svn info filename | Select-String '^Last Changed Date: (.*)$' | ForEach-Object{$_.Matches.Groups[1].Value}

Or taking Ansgars approach without the match (and repeating the label)

(svn info filename) -replace "(?sm).*?^Last Changed Date: (.*?)$.*","`$1"

This might be dodging the question, but you could just use the sed and grep-statements within powershell by importing them from an existing git installation.

If installed, C:\Program Files\Git\usr\bin contains a pretty good selection of the statements you're looking for, such as sed.exe. You can set an alias for these and use them in the way you're used to:

Set-Alias -Name sed -Value C:\"Program Files"\Git\usr\bin\sed.exe

You can define these aliases in a configuration file to preserve them. Create a file called C:\Users\*UserName*\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 and allow script execution in powershell with admin rights by calling:

set-executionpolicy remotesigned
Related