'grep' in PowerShell 1.0

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:

  • Find all files recursively (the -R part).
  • Read each file and print matching lines with their file name.
  • 链接地址: http://www.djcxy.com/p/29020.html

    上一篇: 在.NET Standard 1.5和Visual Studio 2017中使用Apache Lucene.Net 4.8.0

    下一篇: 在PowerShell 1.0中使用'grep'