bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Self contained stream processing scripts


From: david kerns
Subject: Re: Self contained stream processing scripts
Date: Mon, 2 Mar 2020 12:10:12 -0700

It is not my intent to start an editor war here, but within vim, you can
easily swap the syntax high-lighting between languages.
So your shell scripts that have embedded awk programs (within single
quotes) will get properly highlighted with a
:set syntax=awk
Often the shell script part maintains some high-lighting, but to switch back
:set syntax=sh


On Mon, Mar 2, 2020 at 11:55 AM Jonas Møller <address@hidden>
wrote:

> I often use AWK to extract information from, or to convert the output of
> another process to a different format.
>
> Because of this I've often needed to either embed AWK inside a shell
> script, or write a small supplementary shell
> script that calls my AWK script, for example:
>
> > fdisk -l | awk -f my_fdisk_filter.awk
>
> Or, when I really need it to be self-contained:
> > fdisk -l | awk -f <<EOF
> > long heredoc
> > EOF
>
> Embedding AWK inside shell scripts isn't optimal, as editor support for
> multiple languages inside a shell script
> is poor (I suppose you could repurpose Emacs web-mode or similar tools
> for this as well.)
>
> I came up with the following hacky solution:
>
> > @include "shellquote"
> >
> > function popen(cmd,  rdir) {
> >    rdir = ENVIRON["XDG_RUNTIME_DIR"]
> >    if (rdir == "")
> >        rdir = "/tmp" # Should be replaced with a more secure fallback.
> >
> >    __fifo_path = rdir"/gawk_popen."PROCINFO["pid"]".fifo"
> >    system("mkfifo " shell_quote(__fifo_path))
> >    system(cmd " > " shell_quote(__fifo_path) " &")
> >
> >    ARGV[1] = __fifo_path
> >    ARGC = 2
> > }
> >
> > END {
> >    if (__fifo_path != 0)
> >        system("rm " shell_quote(__fifo_path))
> > }
>
> Which can be used like this:
>
> > @include "popen"
> > BEGIN { popen("fdisk -l") }
> > /do/ { print "Some processing on fdisk output" }
>
> I really wish this was a built-in feature, because it allows for more
> self-contained AWK scripts.
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]