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 Tutorial - Find and GREP

Reply
 
Old 12-19-2007   #1 (permalink)
swyck
Guest


 
 

Find and GREP

i've been using FIND and FINDSTR to search through logs or piped
output. It is often compared to the Unix GREP command and I think it
works pretty well.

However, one feature that I've loved with GREP that is missing from
FIND or FINDSTR is the GREP -B switch which prints x lines prior to
the matching line, and -A which prints x lines after the matching
line.

For example say I'm parsing a large log file, and I am searching on
an error message. There may be an explanation of why the error
occurred on the line before (or after) the line with the error
message. That description may not have anything in particular that
can be searched on, or I may not know what it is. With the GREP
switch I can output my match, but I can also include the lines that
give me the information I need.

That's just one example, but there are plenty others. Is there a
relatively simple command I can run that will do the same thing in
Powershell?

My System SpecsSystem Spec
Old 12-19-2007   #2 (permalink)
Mike Miller
Guest


 
 

Re: Find and GREP

Technically, find and findstr aren't part of PowerShell, though they are
likely found on any machine capable of running PowerShell.

If you're already familiar with grep, I suggest installing that. Sourceforge
has a great project which has a number of GNU apps compiled for windows.
http://gnuwin32.sourceforge.net/packages.html

Mike

"swyck" <axlaim@xxxxxx> wrote in message
news:1f4615c1-1706-4f45-bf82-5f91a414deef@xxxxxx
Quote:

> i've been using FIND and FINDSTR to search through logs or piped
> output. It is often compared to the Unix GREP command and I think it
> works pretty well.
>
> However, one feature that I've loved with GREP that is missing from
> FIND or FINDSTR is the GREP -B switch which prints x lines prior to
> the matching line, and -A which prints x lines after the matching
> line.
>
> For example say I'm parsing a large log file, and I am searching on
> an error message. There may be an explanation of why the error
> occurred on the line before (or after) the line with the error
> message. That description may not have anything in particular that
> can be searched on, or I may not know what it is. With the GREP
> switch I can output my match, but I can also include the lines that
> give me the information I need.
>
> That's just one example, but there are plenty others. Is there a
> relatively simple command I can run that will do the same thing in
> Powershell?

My System SpecsSystem Spec
Old 12-19-2007   #3 (permalink)
Kiron
Guest


 
 

Re: Find and GREP

Not in v1. In the CTP of v2 Select-String has a -Context parameter. You could include one line before and after of each match like so:

select-string <pattern> <logfile> -con 1

# ...or see one line before and three after
select-string <pattern> <logfile> -con 1, 3

# for more
man select-string -p context

--
Kiron
My System SpecsSystem Spec
Old 12-19-2007   #4 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: Find and GREP

"Kiron" <Kiron@xxxxxx> wrote in message
news:7BD0B808-AC8D-4CBD-9AEC-DBC610558C50@xxxxxx
Quote:

> Not in v1. In the CTP of v2 Select-String has a -Context parameter. You
> could include one line before and after of each match like so:
>
> select-string <pattern> <logfile> -con 1
>
> # ...or see one line before and three after
> select-string <pattern> <logfile> -con 1, 3
>
> # for more
> man select-string -p context
>
Yeah I would just continue to use findstr until v2 comes out with these new
features on Select-String (including -allmatches and -notmatch - think
grep -v).

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Commad equal to Grep PowerShell
grep -r equiv PowerShell
Grep Vista file management
select-string to act like grep -v PowerShell
Re: grep, which, and tail commands? 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