Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

grep2awk: zsh/zle command transformation function

32 views
Skip to first unread message

Joep van Delft

unread,
Aug 14, 2015, 7:29:52 PM8/14/15
to
Hi,

Being OT while a shamelessly plugging one's own project, is, well,
not done, but I figured some of you might be interested.

I happen to find myself daily losing some time when mechanically
changing a grep command into its awk counterpart. grep2awk is a
zsh/zle function that performs these mechanics correctly with a key
press. This features a transformation of BREs to EREs.

For those interested, please find source and usage instructions at
https://github.com/joepvd/grep2awk.

Kind regards,

Joep

Kaz Kylheku

unread,
Aug 14, 2015, 8:38:05 PM8/14/15
to
You could solve this problem quite differently. Rather than have a robot which
does typing for you, you could make simple shell function which looks like
grep, but also incorporates awk syntax, e.g:

# grok is homonymous with "grawk": grep-awk

grok [grep-opts] pattern [awk-opts] [awk-script] files...

What this will do is transform the grep options and pattern into
an awk pattern/rule combo and combine it with the awk-opts and awk-script.
Then immediately invoke the translation.

Example:

grok -v 'foo' abc.txt

-->

awk -F: '!/foo/ { print }' abc.txt

# --body introduces custom body. Default body is '@@ { print }'
# @@ is replaced with grok's translation of grep pattern.

grok -v 'foo' -F: --body 'BEGIN {x = 42} @@ { counter ++ } { print "blah" }' abc.txt

--->

awk -F: 'BEGIN {x = 42} !/foo/ { counter++ } END { print "blah" }' abc.txt

So initially, you use grok like grep, and you can add some awk logic:
same use case. If you really want to capture the translation, grok
can have a --dump option for that or whatever.

Robots that retype stuff for you are cool, but functional and syntactic
abstraction rules.

One advantage is that after hacking on the awk logic, you still have
the search pattern in grep syntax and can edit it that way.

"Oh, I wanted 'grep -B 2 pattern', not 'grep pattern'." No problem!
Just recall the grok command with all your Awk embellishments
and then and just add -B 2.

With your approach, you have to start a new 'grep -B 2' command, have it
translated to awk all over again, and then merge your previous awk work into
the resulting expansion.

Joep van Delft

unread,
Aug 17, 2015, 4:18:28 AM8/17/15
to
On Sat, 15 Aug 2015 00:38:03 +0000 (UTC)
Kaz Kylheku <k...@kylheku.com> wrote:

> On 2015-08-14, Joep van Delft <joepva...@xs4all.nl> wrote:
> >
> > I happen to find myself daily losing some time when mechanically
> > changing a grep command into its awk counterpart. grep2awk is a
> > zsh/zle function that performs these mechanics correctly with a
> > key press. This features a transformation of BREs to EREs.
> >
> > For those interested, please find source and usage instructions at
> > https://github.com/joepvd/grep2awk.
>
> You could solve this problem quite differently. Rather than have a
> robot which does typing for you, you could make simple shell
> function which looks like grep, but also incorporates awk syntax,
> e.g:
>
> # grok is homonymous with "grawk": grep-awk
>
> grok [grep-opts] pattern [awk-opts] [awk-script] files...
>

Interesting suggestion, thanks.

Well, grep2awk is just a widget to mitigate brain/finger automatisms:
If searching for text in a file, I will type `grep` without thinking.
Often, this puts a (small) burden on the future.

I am not sure if I would like to retrain my brains/fingers to type
`grok` when I already have issues to start off with typing `awk` in
the first place. So I am afraid that the hypothetical `grok` would
not help to alleviate the original problem.

Whether or not `grep` is a function or alias to `grok`, it would
lead to unnecessary confusion, should the need arise to share parts
of my PTY session with others.

Kind regards,

Joep


0 new messages