Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Tip of the day: grep2awk zle function



On Sun, 16 Aug 2015 11:38:00 +0000 (UTC)
zzapper <david@xxxxxxxxxxxxxx> wrote:

> > Code, installation instructions, and some details can be found at
> > https://github.com/joepvd/grep2awk. 
>
> Sounds interesting do you have any typical before & after examples?
> 

A yeah, examples make sense, thanks!

After placing the file grep2awk in $zpath, type:

    % autoload -Uz grep2awk
    % zle -N grep2awk
    % bindkey "^X^A" grep2awk

Then, if you press the key combination, the editing buffer will be
searched for grep commands, and the buffer will have the grep part
replaced by an awk command. 

    % grep 'there^here'<CTRL-X><CTRL-A>
    % awk -- '/there\^here/ {print $0}'

It works with pipes, subshells, et cetera (thanks to
split-shell-arguments):

    % ps aux | grep kswap | sort<CTRLX><CTRL-A>
    % ps aux | awk -- '/kswap/ {print $0}' | sort

The meat of this thing is in the transformation of BREs to EREs (when
egrep or -E is not called): 

    % grep '^a^b\(c(\|d)e\)' file<CTRL-X><CTRL-A>
    % awk -- '/^a\^b(c\(|d\)e)/ {print $0}' file

Some options to grep will be translated to appropriate awk statements:

    % grep -vi 'not here' file <CTRLX><CTRL-A>
    % awk -- 'BEGIN{IGNORECASE=1}; !/not here/ {print $0}' file

Kind regards,

Joep




Messages sorted by: Reverse Date, Date, Thread, Author