In awk, you can perform an action for a given pattern, like:
echo foo | awk '/foo/ {print "foo"}'
or you can perform an action at the end of the input, like:
echo foo | awk 'END {print "END"}'
But it does not appear to be possible to do both, like:
# echo foo | awk '/foo/ || END {print "foo or END"}'
awk: syntax error at source line 1
context is
/foo/ || >>> END <<< {print "foo or END"}
awk: bailing out at source line 1
Is this possible?
No. Do this instead:
awk '
/foo/ { prtInfo() }
END { prtInfo() }
function prtInfo() { print "foo or END" }
'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With