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 - select-string to act like grep -v

Reply
 
Old 05-11-2007   #1 (permalink)
Frank


 
 

select-string to act like grep -v

Does anyone know how to use select-string to print out everything except some
string. ie. grep -v dontwantstring file.txt.

Thanks in advance,



My System SpecsSystem Spec
Old 05-11-2007   #2 (permalink)
Brandon Shell


 
 

Re: select-string to act like grep -v

Here is my Text file:
1) This is a test
2) To see if I can do grep -v
3) with select string Test

If you want to use select-string you can use the ! operator

48# cat mytext.txt | select-string "test"
1) This is a test
3) with select string Test

49# cat mytext.txt | ?{!($_ | select-string "test")}
cat mytext.txt | ?{!($_ | select-string "test")}

I am sure there is an easier way to accomplish what you want, but I am on
the bus ATM and cant poke around much.

"Frank" <Frank@discussions.microsoft.com> wrote in message
news:160AA7EC-722B-4CA5-A64D-B29F51465CAA@microsoft.com...
> Does anyone know how to use select-string to print out everything except
> some
> string. ie. grep -v dontwantstring file.txt.
>
> Thanks in advance,
>
>


My System SpecsSystem Spec
Old 05-11-2007   #3 (permalink)
hecks@hotmail.co.uk


 
 

Re: select-string to act like grep -v

On May 11, 11:28 pm, Frank <F...@discussions.microsoft.com> wrote:
> Does anyone know how to use select-string to print out everything except some
> string. ie. grep -v dontwantstring file.txt.
>
> Thanks in advance,


If you mean literally everything except the string, then this seems
quicker:

(cat file.txt) - replace "dontwantstring"

-Hecks

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


 
 

Re: select-string to act like grep -v

"Frank" <Frank@discussions.microsoft.com> wrote in message
news:160AA7EC-722B-4CA5-A64D-B29F51465CAA@microsoft.com...
> Does anyone know how to use select-string to print out everything except
> some
> string. ie. grep -v dontwantstring file.txt.


It might be nice if Select-String supported a -NotMatch parameter but it
doesn't. So I would do this:

PS> gc file.txt | ?{$_ -notmatch 'dontwantstring'}

--
Keith


My System SpecsSystem Spec
Old 05-12-2007   #5 (permalink)
hecks@hotmail.co.uk


 
 

Re: select-string to act like grep -v

On May 12, 5:25 am, "Keith Hill [MVP]"
<r_keith_h...@no.spam.thank.u.hotmail.com> wrote:
> "Frank" <F...@discussions.microsoft.com> wrote in message
>
> news:160AA7EC-722B-4CA5-A64D-B29F51465CAA@microsoft.com...
>
> > Does anyone know how to use select-string to print out everything except
> > some
> > string. ie. grep -v dontwantstring file.txt.

>
> It might be nice if Select-String supported a -NotMatch parameter but it
> doesn't. So I would do this:
>
> PS> gc file.txt | ?{$_ -notmatch 'dontwantstring'}
>
> --
> Keith


But again that will only return the lines that don't contain the
string, is that what the OP wants? Using the example above, (gc
file.txt) -replace "test" will return:

1) This is a
2) To see if I can do grep -v
3) with select string

-Hecls

My System SpecsSystem Spec
Old 05-12-2007   #6 (permalink)
Keith Hill [MVP]


 
 

Re: select-string to act like grep -v

<hecks@hotmail.co.uk> wrote in message
news:1178945469.398505.130210@k79g2000hse.googlegroups.com...
>
> But again that will only return the lines that don't contain the
> string, is that what the OP wants? Using the example above, (gc
> file.txt) -replace "test" will return:
>
> 1) This is a
> 2) To see if I can do grep -v
> 3) with select string


I think so. The definition of grep -v is:

-v

displays all lines not matching a pattern.

--
Keith


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Select-String -exclude PowerShell
select-string substitute PowerShell
problems with $var | select-string -pattern $string -q PowerShell
Re: select-string exceptions PowerShell
select-string question 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