在PowerShell 1.0中使用'grep' [英] 'grep' in PowerShell 1.0

查看:239
本文介绍了在PowerShell 1.0中使用'grep'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件的任何位置查找包含单词navbar的文件。我可以使用Mac的grep命令来做到这一点:

  grep -Rnavbar* 

$ p $在$ PowerShell 1.0中的等价物是什么?

解决方案

  findstr / snavbar* 

这是一个本地命令,但应该工作得很好。



PowerShell 1.0本身有点棘手,例如 Select-String 直接相当于)自2.0以来才存在,我想。所以你必须做一些类似的事情:

  Get-ChildItem -Recurse | 
ForEach-Object {
$ file = $ _
($ _ | Get-Content)-cmatch'navbar'|
ForEach-Object {$ file.Name +':'+ $ _}
}

短版:

  ls -r |%{$ f = $ _;($ _ | gc) - cmatch'navbar'|%{$ f.Name +:$ _}} 

从字面上看:


  1. 递归地查找所有文件( -R 部分) li>
  2. 阅读每个文件并用它们的文件名打印匹配行。


I want to find files containing the word "navbar" anywhere in files. I can do this using Mac's grep command like this:

grep -R "navbar" *

What's its equivalent in PowerShell 1.0?

解决方案

findstr /s "navbar" *

It's a native command but should work well enough.

PowerShell 1.0 itself is a little tricky, as Select-String (the direct equivalent) only exists since 2.0, I think. So you'd have to make do with something like:

Get-ChildItem -Recurse |
  ForEach-Object {
    $file = $_
    ($_ | Get-Content) -cmatch 'navbar' |
      ForEach-Object { $file.Name + ':' + $_ }
  }

Short version:

ls -r|%{$f=$_;($_|gc)-cmatch'navbar'|%{$f.Name+":$_"}}

This is quite literally:

  1. Find all files recursively (the -R part).
  2. Read each file and print matching lines with their file name.

这篇关于在PowerShell 1.0中使用'grep'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆