Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Re: grep, which, and tail commands?

Reply
 
Old 07-05-2006   #1 (permalink)
Jonathan Eric Miller


 
 

Re: grep, which, and tail commands?

Thanks for the response Andrew.

"Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
news:lmjka2169ebv0h0tec4mfl7147iqo0pfb8@4ax.com...
> Jon,
>
> For grep equivalence take a look at get-content with a where-object in
> the second pipeline step.
>
> For example, you want to find lines in a file that match a regular
> expression pattern:
>
> get-content filename |
> where-object {$_ -match "regularExpressionPattern"}


Would it be easy to search multiple files and print the names/paths of the
files that contain a match?

> Adapt as necessary.
>
> For tail take a look at the -first and -last parameters on
> select-object. You would typically use it with a sort-object in the
> preceding pipeline step to order objects according to the criterion of
> interest.


I don't know if this would work because what "tail -f" does is monitor the
file for changes. Everytime text is appended to the file, tail prints the
new text out. I was able to implement this using FileSystemWatcher in .NET.

> I am not too familiar with "which" but from your description something
> like


Actually, I found that Get-Command seems to do exactly what UNIX which does.
Basically, all you have to do is the following.

PS C:\> get-command ping

CommandType Name Definition
----------- ---- ----------
Application ping.exe C:\WINDOWS\system32\ping.exe

> get-childitem Script1.ps1 |
> format-list Name, PSParentPath
>
> should give you something similar to what you want.
>
> Andrew Watt MVP
> Author - Professional Windows PowerShell (Wrox)
>
> On Mon, 3 Jul 2006 14:37:01 -0700, Jon Miller <Jon
> Miller@discussions.microsoft.com> wrote:
>
>>Hi,
>>
>>I'm just starting to play around with PowerShell. It seems pretty cool.
>>There are a few things that I'm wondering though. Namely, I'm wondering if
>>there are equivalents for UNIX commands grep, which, and tail. With regard
>>to
>>grep, I'm wondering if there are plans for an updated version of
>>FINDSTR.EXE
>>that is possibly more robust. I've also found the UNIX which command very
>>valuable. It tells you where in your path an executable file is found. And
>>also, tail with the -f flag is very valuable for viewing log files and
>>having
>>their output displayed as new output is written to them. I've created my
>>own
>>.NET implementations (in C#) of the later two commands, but, I was
>>thinking
>>it would be nice since there seems to be interest in finally getting a
>>decent
>>shell going for Windows that these other commands be bundled with the OS
>>and/or the shell.
>>
>>Jon




My System SpecsSystem Spec
Old 07-05-2006   #2 (permalink)
Andrew Watt [MVP]


 
 

Re: grep, which, and tail commands?

On Wed, 5 Jul 2006 15:12:39 -0500, "Jonathan Eric Miller"
<jemiller@uchicago.edu> wrote:

>Would it be easy to search multiple files and print the names/paths of the
>files that contain a match?


Here you go. Adapt as necessary.

It shows filenames which contain one or more matches and also shows
the line which contains a match.

$files = get-childitem [fr]*.txt
foreach ($file in $files)
{
if (get-content $file | where-object {$_ -match "red"})
{
write-host "`n"
write-host "$file" -ForegroundColor Yellow -Backgroundcolor Black
}
$lines = get-content $file
foreach ($line in $file)
{ get-content $file | where-object {$_ -match "red"}
}

}

Andrew Watt MVP
My System SpecsSystem Spec
Old 07-05-2006   #3 (permalink)
Jon Miller


 
 

Re: grep, which, and tail commands?

Thanks

"Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
news:gf9oa29bj0k53bdrv4if23rdv8ef4dggvh@4ax.com...
> On Wed, 5 Jul 2006 15:12:39 -0500, "Jonathan Eric Miller"
> <jemiller@uchicago.edu> wrote:
>
>>Would it be easy to search multiple files and print the names/paths of the
>>files that contain a match?

>
> Here you go. Adapt as necessary.
>
> It shows filenames which contain one or more matches and also shows
> the line which contains a match.
>
> $files = get-childitem [fr]*.txt
> foreach ($file in $files)
> {
> if (get-content $file | where-object {$_ -match "red"})
> {
> write-host "`n"
> write-host "$file" -ForegroundColor Yellow -Backgroundcolor Black
> }
> $lines = get-content $file
> foreach ($line in $file)
> { get-content $file | where-object {$_ -match "red"}
> }
>
> }
>
> Andrew Watt MVP



My System SpecsSystem Spec
Old 07-06-2006   #4 (permalink)
Jason Gurtz


 
 

Re: grep, which, and tail commands?

Jonathan Eric Miller wrote:
> PS C:\> get-command ping
>
> CommandType Name Definition
> ----------- ---- ----------
> Application ping.exe C:\WINDOWS\system32\ping.exe


....tho the output is unnecessarily verbose and doesn't seem suitable for
inclusion in a pipeline of commands.

I guess a combination of grep \\ | awk "{print $3}" could alleviate
that, but a -q flag would be better or better yet, quiet by default and
have -v or -h for verbosity or human readable output.

~Jason

--
My System SpecsSystem Spec
Old 07-06-2006   #5 (permalink)
/\\/\\o\\/\\/


 
 

Re: grep, which, and tail commands?

Jason Gurtz wrote:
> Jonathan Eric Miller wrote:
>> PS C:\> get-command ping
>>
>> CommandType Name Definition
>> ----------- ---- ----------
>> Application ping.exe C:\WINDOWS\system32\ping.exe

>
> ...tho the output is unnecessarily verbose and doesn't seem suitable for
> inclusion in a pipeline of commands.
>
> I guess a combination of grep \\ | awk "{print $3}" could alleviate
> that, but a -q flag would be better or better yet, quiet by default and
> have -v or -h for verbosity or human readable output.
>
> ~Jason
>


You mean like this ? :

(get-command ping).Definition

or

get-command ping | select definition
get-command ping | ft definition


Greetings /\/\o\/\/
My System SpecsSystem Spec
Old 07-06-2006   #6 (permalink)
Jason Gurtz


 
 

Re: grep, which, and tail commands?

/\/\o\/\/ wrote:
> You mean like this ? :
>
> (get-command ping).Definition



Oh, right on. The OO technique is even better!

Cheers,

~Jason

--
My System SpecsSystem Spec
Old 07-06-2006   #7 (permalink)
Jon Miller


 
 

Re: grep, which, and tail commands?

Yeah, I think that's supposed to be one of the major differences between
PowerShell and UNIX shells, namely, it uses objects and not just text
output.

"/\/\o\/\/" <no@spam.mow> wrote in message
news:O03JV9RoGHA.4340@TK2MSFTNGP05.phx.gbl...
> Jason Gurtz wrote:
>> Jonathan Eric Miller wrote:
>>> PS C:\> get-command ping
>>>
>>> CommandType Name Definition
>>> ----------- ---- ----------
>>> Application ping.exe
>>> C:\WINDOWS\system32\ping.exe

>>
>> ...tho the output is unnecessarily verbose and doesn't seem suitable for
>> inclusion in a pipeline of commands.
>>
>> I guess a combination of grep \\ | awk "{print $3}" could alleviate
>> that, but a -q flag would be better or better yet, quiet by default and
>> have -v or -h for verbosity or human readable output.
>>
>> ~Jason
>>

>
> You mean like this ? :
>
> (get-command ping).Definition
>
> or
>
> get-command ping | select definition
> get-command ping | ft definition
>
>
> Greetings /\/\o\/\/



My System SpecsSystem Spec
Old 07-06-2006   #8 (permalink)
Bruce Payette [MSFT]


 
 

Re: grep, which, and tail commands?

I seem to be coming in the tail end of this thread for some reason. Has
select-string been discussed? This is our grep equivalent. If you want to
search through a directory of files, then you can do

select-string pattern *.txt

To search through a file heirarchy do

dir -rec -include *.txt | select-string pattern

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.



"Jonathan Eric Miller" <jemiller@uchicago.edu> wrote in message
news:%23Yrko9GoGHA.1592@TK2MSFTNGP04.phx.gbl...
> Thanks for the response Andrew.
>
> "Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
> news:lmjka2169ebv0h0tec4mfl7147iqo0pfb8@4ax.com...
>> Jon,
>>
>> For grep equivalence take a look at get-content with a where-object in
>> the second pipeline step.
>>
>> For example, you want to find lines in a file that match a regular
>> expression pattern:
>>
>> get-content filename |
>> where-object {$_ -match "regularExpressionPattern"}

>
> Would it be easy to search multiple files and print the names/paths of the
> files that contain a match?
>
>> Adapt as necessary.
>>
>> For tail take a look at the -first and -last parameters on
>> select-object. You would typically use it with a sort-object in the
>> preceding pipeline step to order objects according to the criterion of
>> interest.

>
> I don't know if this would work because what "tail -f" does is monitor the
> file for changes. Everytime text is appended to the file, tail prints the
> new text out. I was able to implement this using FileSystemWatcher in
> .NET.
>
>> I am not too familiar with "which" but from your description something
>> like

>
> Actually, I found that Get-Command seems to do exactly what UNIX which
> does. Basically, all you have to do is the following.
>
> PS C:\> get-command ping
>
> CommandType Name Definition
> ----------- ---- ----------
> Application ping.exe
> C:\WINDOWS\system32\ping.exe
>
>> get-childitem Script1.ps1 |
>> format-list Name, PSParentPath
>>
>> should give you something similar to what you want.
>>
>> Andrew Watt MVP
>> Author - Professional Windows PowerShell (Wrox)
>>
>> On Mon, 3 Jul 2006 14:37:01 -0700, Jon Miller <Jon
>> Miller@discussions.microsoft.com> wrote:
>>
>>>Hi,
>>>
>>>I'm just starting to play around with PowerShell. It seems pretty cool.
>>>There are a few things that I'm wondering though. Namely, I'm wondering
>>>if
>>>there are equivalents for UNIX commands grep, which, and tail. With
>>>regard to
>>>grep, I'm wondering if there are plans for an updated version of
>>>FINDSTR.EXE
>>>that is possibly more robust. I've also found the UNIX which command very
>>>valuable. It tells you where in your path an executable file is found.
>>>And
>>>also, tail with the -f flag is very valuable for viewing log files and
>>>having
>>>their output displayed as new output is written to them. I've created my
>>>own
>>>.NET implementations (in C#) of the later two commands, but, I was
>>>thinking
>>>it would be nice since there seems to be interest in finally getting a
>>>decent
>>>shell going for Windows that these other commands be bundled with the OS
>>>and/or the shell.
>>>
>>>Jon

>
>



My System SpecsSystem Spec
Old 07-06-2006   #9 (permalink)
Jason Gurtz


 
 

Re: grep, which, and tail commands?

Bruce Payette [MSFT] wrote:
> select-string pattern *.txt


I'm kinda interested if anyone could comment on the merits/differences
of using select-string's -path arg with globing vs. get-childitem

> To search through a file heirarchy do
>
> dir -rec -include *.txt | select-string pattern


Esp. -rec offers a lot of power. Glad to say goodbye to du | grep | awk
combos! A nice addition for PSH v2 might be a "maxDepth" value to the
-recurse. I get this idea from Dan Cross' walk program written for
Plan9 [1]

~Jason

--
[1]
Description (w/ C code):
<http://lists.cse.psu.edu/archives/9fans/2002-October/020820.html>
and Usage:
<http://lists.cse.psu.edu/archives/9fans/2002-October/020822.html>
My System SpecsSystem Spec
Old 07-06-2006   #10 (permalink)
Jon Miller


 
 

Re: grep, which, and tail commands?

Cool, thanks Bruce. I'll try it out.

On an unrelated note, I think I may have found a bug in get-command (not
sure where I would report this). If you do the following, you will get back
files that aren't executable but are in the path. For example, the following
command returns C:\WINDOWS\system32\setup.bmp among others.

gcm set*

Jon

"Bruce Payette [MSFT]" <brucepay@microsoft.com> wrote in message
news:%23DiMIRSoGHA.4340@TK2MSFTNGP05.phx.gbl...
>I seem to be coming in the tail end of this thread for some reason. Has
>select-string been discussed? This is our grep equivalent. If you want to
>search through a directory of files, then you can do
>
> select-string pattern *.txt
>
> To search through a file heirarchy do
>
> dir -rec -include *.txt | select-string pattern
>
> -bruce
>
> --
> Bruce Payette [MSFT]
> Windows PowerShell Technical Lead
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>
>
> "Jonathan Eric Miller" <jemiller@uchicago.edu> wrote in message
> news:%23Yrko9GoGHA.1592@TK2MSFTNGP04.phx.gbl...
>> Thanks for the response Andrew.
>>
>> "Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
>> news:lmjka2169ebv0h0tec4mfl7147iqo0pfb8@4ax.com...
>>> Jon,
>>>
>>> For grep equivalence take a look at get-content with a where-object in
>>> the second pipeline step.
>>>
>>> For example, you want to find lines in a file that match a regular
>>> expression pattern:
>>>
>>> get-content filename |
>>> where-object {$_ -match "regularExpressionPattern"}

>>
>> Would it be easy to search multiple files and print the names/paths of
>> the files that contain a match?
>>
>>> Adapt as necessary.
>>>
>>> For tail take a look at the -first and -last parameters on
>>> select-object. You would typically use it with a sort-object in the
>>> preceding pipeline step to order objects according to the criterion of
>>> interest.

>>
>> I don't know if this would work because what "tail -f" does is monitor
>> the file for changes. Everytime text is appended to the file, tail prints
>> the new text out. I was able to implement this using FileSystemWatcher in
>> .NET.
>>
>>> I am not too familiar with "which" but from your description something
>>> like

>>
>> Actually, I found that Get-Command seems to do exactly what UNIX which
>> does. Basically, all you have to do is the following.
>>
>> PS C:\> get-command ping
>>
>> CommandType Name Definition
>> ----------- ---- ----------
>> Application ping.exe C:\WINDOWS\system32\ping.exe
>>
>>> get-childitem Script1.ps1 |
>>> format-list Name, PSParentPath
>>>
>>> should give you something similar to what you want.
>>>
>>> Andrew Watt MVP
>>> Author - Professional Windows PowerShell (Wrox)
>>>
>>> On Mon, 3 Jul 2006 14:37:01 -0700, Jon Miller <Jon
>>> Miller@discussions.microsoft.com> wrote:
>>>
>>>>Hi,
>>>>
>>>>I'm just starting to play around with PowerShell. It seems pretty cool.
>>>>There are a few things that I'm wondering though. Namely, I'm wondering
>>>>if
>>>>there are equivalents for UNIX commands grep, which, and tail. With
>>>>regard to
>>>>grep, I'm wondering if there are plans for an updated version of
>>>>FINDSTR.EXE
>>>>that is possibly more robust. I've also found the UNIX which command
>>>>very
>>>>valuable. It tells you where in your path an executable file is found.
>>>>And
>>>>also, tail with the -f flag is very valuable for viewing log files and
>>>>having
>>>>their output displayed as new output is written to them. I've created my
>>>>own
>>>>.NET implementations (in C#) of the later two commands, but, I was
>>>>thinking
>>>>it would be nice since there seems to be interest in finally getting a
>>>>decent
>>>>shell going for Windows that these other commands be bundled with the OS
>>>>and/or the shell.
>>>>
>>>>Jon

>>
>>

>
>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Commad equal to Grep PowerShell
Find and GREP PowerShell
grep -r equiv PowerShell
Grep Vista file management
select-string to act like grep -v PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46