[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Self contained stream processing scripts
From: |
arnold |
Subject: |
Re: Self contained stream processing scripts |
Date: |
Tue, 03 Mar 2020 00:37:03 -0700 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
Hi.
Thank you for your suggestion.
What you're looking for is a way to hook the output of a command into
gawk's main input loop via ARGV.
Your solution is really not that much of a hack, but a fairly clean
way to build upon existing facilities in awk and in the underlying OS
to get what you need, which is in keeping with the the best of Unix
tradition and style.
I don't plan to add something like this built-in to gawk, especially as
you've shown it can be done (portably, too!) in under 30 lines of code.
With respect to embedding awk scripts in shell scripts, I do that all
the time when the scripts are small. Besides providing the script
as a here document or a long quoted string, you can also do something
like:
cat << \EOF > /tmp/x.$$.awk
# awk script here
EOF
trap EXIT HUP TERM "rm -f /tmp/x.$$.awk"
awk -f /tmp/x.$$.awk ....
Thanks,
Arnold
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.