pawk icon indicating copy to clipboard operation
pawk copied to clipboard

Multiple regex matches

Open JJK96 opened this issue 3 years ago • 5 comments

Feature request to enable things like:

cat /etc/hosts | pawk '!/^#/ and !/^127'

This would require a whole new way of parsing. It should parse it as a python expression with some syntactic sugar for regex matches.

JJK96 avatar Oct 20 '22 14:10 JJK96

Maybe replace the regexes in the string by re.match(r"<regex>", ...). Then later eval it as an expression.

JJK96 avatar Oct 20 '22 14:10 JJK96

Would this do what you want?

cat /etc/hosts | pawk '!/^#|^127/'

dwurf avatar Oct 20 '22 23:10 dwurf

In this example, yes, but I think a general solution is useful, because sometimes you want to make it match 2 regexes after each other with an and relation. Example:

$ cat meetings
Bob meets Alice
Charlie meets Dave
Dave meets Alice
Alice meets Bob
$ cat meetings | pawk '/Bob/ and /Alice/'
Alice meets Bob
Bob meets Alice

Awk allows this with the && operator:

cat meetings.txt | awk '/Bob/&&/Alice/'
Alice meets Bob
Bob meets Alice

JJK96 avatar Oct 21 '22 05:10 JJK96

Why not just run it through pawk twice?

$ cat meetings.txt | pawk '/Bob/' | pawk '/Alice/'

it's only a few characters longer than the syntax you're suggesting

MegaBluejay avatar Oct 22 '22 07:10 MegaBluejay

At the moment I can't think of a good example, I'll get back to it if I encounter one. Thanks for the suggestions and for making this tool!

JJK96 avatar Oct 23 '22 10:10 JJK96