Multiple -notmatch strings in powershell

I am struggeling to combine -notmatch expressions using powershell, just like what I do under Linux e.g. with grep -v "/aaa/" | grep -v "/bbb/". In other words I have the input in.txt looking as follows:

/aaa
/aaa/x.txt
/bbb
/bbb/y.txt
/ccc
/ccc/z.txt

I want to end up with an file out.txt

/aaa
/bbb
/ccc
/ccc/z.txt

What I have so far is cat in.txt | ?{$_ -notmatch '/aaa/'} > out.txt;, but how do I generalize this?

PS: Yes, I searched the web for examples or manpage, but I guess I am stuck in my bubble.

References

1 Answer

Pipe a second -notmatch:

cat in.txt | ?{$_ -notmatch '/aaa/'} | ?{$_ -notmatch '/bbb/'} > out.txt;

Or to shorten it:

cat in.txt | ?{$_ -notmatch '/aaa/|/bbb/'} > out.txt;

It should produce your expected result.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like