bug-gawk
[Top][All Lists]
Advanced

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

Self contained stream processing scripts


From: Jonas Møller
Subject: Self contained stream processing scripts
Date: Mon, 02 Mar 2020 19:03:22 +0100
User-agent: Roundcube Webmail/1.1.10

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]