grep and sed equivalent in PowerShell

孤街浪徒 提交于 2020-01-03 12:33:28

问题


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

回答1:


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"



回答2:


Use the -match and -replace operators:

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


来源:https://stackoverflow.com/questions/53867147/grep-and-sed-equivalent-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!