alecthomas / pawk

PAWK - A Python line processor (like AWK)
MIT License
517 stars 32 forks source link

Multiple regex matches #20

Open JJK96 opened 2 years ago

JJK96 commented 2 years ago

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 commented 2 years ago

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

dwurf commented 2 years ago

Would this do what you want?

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

JJK96 commented 2 years ago

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
MegaBluejay commented 2 years ago

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

JJK96 commented 2 years ago

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!