20

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
JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
script0207
  • 345
  • 1
  • 4
  • 12

3 Answers3

20

Use the -match and -replace operators:

(svn info filename) -match '^Last Changed Date:' -replace '^Last Changed Date: '
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
17

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
Marvin Schmidt
  • 171
  • 1
  • 3
  • yessssss. `"some string" | & 'C:\Program Files\git\usr\bin\sed.exe' -e 's/^some/any/'` -> `any string`. or with the alias, you can drop the `&` - `'some string' | sed -e 's/^some/any/'` – orion elenzil Mar 04 '22 at 18:29
15

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"
Andrew
  • 3,912
  • 17
  • 28
  • 1
    the above is returning this "2017-05-05 12:03:02 -0400 (Fri, 05 May 2017)". what time format is this in powershell?? i need the same format to compare with today time in powershell. – script0207 Dec 20 '18 at 12:05
  • `-replace` without `-match` will not remove non-matching lines from the `svn` output (same as using `sed` without `grep`). – Ansgar Wiechers Dec 20 '18 at 12:22
  • @AnsgarWiechers I agree that if no match is found the whole output is returned unchanged, but IMO that seems quite unlikely in this case. –  Dec 20 '18 at 12:33