【问题标题】:What is the PowerShell equivalent to this Bash command?与此 Bash 命令等效的 PowerShell 是什么?
【发布时间】:2011-01-07 08:29:13
【问题描述】:
我正在尝试创建一个CLI 命令让TFS 检查所有包含特定字符串的文件。我主要使用Cygwin,但tf 命令在Cygwin 环境中运行时无法解析路径。
我认为 PowerShell 应该能够做同样的事情,但我不确定 grep 和 xargs 的等效命令是什么。
那么,与以下 Bash 命令等效的 PowerShell 版本是什么?
grep -l -r 'SomeSearchString' . | xargs -L1 tf edit
【问题讨论】:
标签:
powershell
grep
xargs
【解决方案1】:
在 PowerShell 中使用一些 UNIX 别名(如 ls):
ls -r | select-string 'SomeSearchString' | Foreach {tf edit $_.Path}
或更规范的 Powershell 形式:
Get-ChildItem -Recurse | Select-String 'SomeSearchString' |
Foreach {tf edit $_.Path}
并使用 PowerShell 别名:
gci -r | sls 'SomeSearchString' | %{tf edit $_.Path}
【解决方案2】:
我发现使用变量更容易理解,例如,
PS> $files = Get-ChildItem -Recurse |
Select-String 'SomeSearchString' |
%{$_.path} |
Select -Unique
PS> tf edit $files