msh: Extract text from a stream with grep-string

Contributor Icon Contributed by qmchenry Date Icon August 30, 2004  
Tag Icon Tagged: Windows Monad msh

One of the biggest missing functions in the command prompt is the ability to extract lines from a file that match a pattern. For UNIX users, the command grep is so often used that it has become a verb. The msh has a grep-string cmdlet that performs grep-like string parsing.


Use grep-string to search for a text pattern in a stream of text, for example:

dir | grep-string txt

will extract lines from the dir command output containing the text txt. For more information about the use of grep-string, use the help cmdlet:

help grep-string

Unfortunately, it appears that Microsoft used the name grep without really meaning it. Grep stands for global regular expression parser and its greatest strength is the ‘regular expression’ part. The msh grep-string doesn’t use regular expressions. One promising thought is that since these are all defined cmdlets, someone adventurous could write a real grep function.

Previous recipe | Next recipe |
 
  • Anonymous
    I regularly (heh) use dir/s/b | findstr /f:/ /r "^my.*reg[a-z][^0-9]expr>" in the Windows cmd shell
  • Anonymous
    Sadly, it would appear that grep-string is not a part of MSH Beta 3:

    [C:]: dir | grep-string
    'grep-string' is not recognized as a Cmdlet, function, operable program, or script file.
    At line:1 char:17
    + dir | grep-string <<<<
    [C:]:



    Thomas
    (http://tfl09.blogspot.com)
  • Anonymous
    Thomas,
    I didn't use beta 3, so I don't know if its the same, but there is now the select-string cmdlet (as of PowerShell RC1)
    If it is the same, you'd probably already know about it (since you're blog has quite a bit of info on msh(PowerShell)

    However, for others passing through, I thought I'd post this example:

    > set-content test.txt "Hello World"
    > dir *.txt|select-string ^H.*d$
    test.txt:1:Hello World
blog comments powered by Disqus