script0207
script0207

Reputation: 395

grep and sed equivalent in PowerShell

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

Upvotes: 25

Views: 70319

Answers (3)

user6811411
user6811411

Reputation:

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"

Upvotes: 16

Marvin Schmidt
Marvin Schmidt

Reputation: 191

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

Upvotes: 19

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200453

Use the -match and -replace operators:

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

Upvotes: 23

Related Questions