This is a bit tricky; I'm trying to work out the best approach to this problem. I have a couple of approaches, but they seem really hacky and I'd like something a little more elegant.
I want to parse a whitespace delimited file, ignoring #comment lines
and complaining of any non-empty lines that don't have exactly 4 fields. This is easy enough in awk
:
awk '/^#/ {next}; NF == 0 {next}; NF != 4 {exit 1}; (dostuff)'
The trick is what I want to do with the data, is actually set it as variables in bash
and then run a bash
function, unless $2 contains a specific value.
Here is some pseudocode (mostly real but mixed languages) to explain what I mean:
# awk
/^#/ {next}
NF == 0 {next}
NF != 4 {exit 1}
$2 == "manual" {next}
# bash
NAME=$1
METHOD=$2
URL=$3
TAG=$4
complicated_bash_function_that_calls_lots_of_external_commands
# then magically parse the next line with awk.
I don't know how to do this without some ugly workarounds, such as calling awk
or sed
separately for each line of the file. (Originally I put the question as "How to call bash function from within awk or each output line of awk from within bash?")
Possibly it would work to modify the bash function into its own script, and make it accept arguments 1, 2, 3, 4 as above. I'm not sure how to call that from within awk, though; hence my question title.
What I would actually prefer to do, is have the whole thing in one file and make it a bash script - calling awk
from within bash
rather than bash
from within awk
. But I will still need to call the bash
function from within awk--once for each non-comment line of the input file.
How can I do this?
while IFS= read -r
loop or similar?system()
command inawk
.system()
command just executes the command in a subshell and returns to awk, the output of that command may even be mangled with the output of your awk script in some funny cases unless you usefflush("/dev/stdout")
. If you need to parse the output of the command you need to use the| getline
syntax.