Grep is one of my favorite tools to help me find something in a set of files. Since I cannot download Cygwin at work, I have to make due with what I have.
The following is a translation of grep -R "mypattern" *.cpp for Powershell.
gci C:\path\to\files\* --include *.cpp -recurse | select-string -pattern "mypattern" -caseSensitive
<sarcasm>
So much easier to remember…
</sarcasm>








11 responses so far ↓
1 Shiv // Jan 16, 2008 at 5:41 pm
Perfect! Just what I was looking for, thanks!
2 Ben // Apr 6, 2008 at 11:14 pm
Great!
Powershell syntax makes perfect sense when looked at from a “implementors” point of view, but from a practical standpoint…it’s rather ugly.
3 Eddie // Apr 20, 2008 at 4:26 am
Or you could use findstr
4 Smiley // Apr 26, 2008 at 8:55 pm
Findstr works well, as Eddie said.
To make it more natural, I made a new alias called grep and make it execute findstr.
“new-alias grep findstr”
and now I can follow my instincts.
5 Marshall // Apr 26, 2008 at 10:16 pm
findstr works, but it is not Powershell specific. You can run findstr on cmd.exe on Windows 98 if you want. I was looking for a grep-like command that was 100% Powershell.
6 IGuy // Jul 28, 2008 at 5:06 pm
just do this:
function grep($pattern,$filepath=”*.*”)
{
Get-ChildItem .\* -Include $filepath -Recurse | Select-String -Pattern $pattern -casesensitive
}
In your $profile…
Not perfect but close enough for my needs at the moment.
7 Emulating Grep in Powershell | Tech Space // Mar 16, 2009 at 1:36 pm
[...] Grep in Powershell Posted by Thomas Powell – 16/03/09 at 02:03 pm Emulating Grep in Powershell. The option presented is to perform a grep on several files.For a search in one file, with results [...]
8 Bob // Apr 9, 2009 at 12:04 am
How on Earth did PS make it out the door (several times now) without a ‘grep’? A line based pattern matching routine that is 100% PS based seems like a no-brainer deliverable for any 1.0 release of a scripting language. FINDSTR is so archane.
9 Klas Mellbourn // Apr 22, 2009 at 5:52 am
Rather than ugly, I think that PowerShell in this case is clean and elegant.
If there is a problem, it is that PowerShell is *too* clean.
The principle the language designers have followed here is that each command should have a very well specified, particular, isolated task.
Listing files is one such task.
Finding strings is another task.
“grep” is not clean or elegant in this sense, since it does these two tasks in combination.
The following is often comparable to grep, and not too long:
gci . | select-string “foo”
10 Emulating grep with PowerShell - Software Warlock // May 7, 2009 at 2:37 pm
[...] this took a little poking around the Internet, but ultimately I found a great blog post describing how to do it. You need to to use a combination of get-childitem (abbreviated gci) and [...]
11 voretaq7 // Sep 11, 2009 at 3:44 pm
Your grep command doesn’t match your powershell command (at least not with the grep on FreeBSD) – Your grep recursively searches anything named “*.cpp”, your powershell recursively finds files named “*.cpp” and searches them.
That’s still an ugly version of grep “mypattern’ `find . -name “*.cpp”` though….
(Related for Klas: grep is not “listing filenames” in the example – “*.cpp” is an explicit wildcard list of files to be searched.)
Leave a Comment