Multiple -Notmatch Strings in Powershell

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

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

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.