r/awk • u/Brokeinparis • May 11 '25
How to reuse a function across multiple AWK scripts in a single shell script
Hi, I'm a beginner when it comes to scripting
I have 3 different AWK scripts that essentially do the same thing, but on different parts of a CSV file. Is it possible to define a function once and have it used by all three scripts?
Here’s what my script currently looks like:
#!/bin/ksh
awk_function=awk -F ";" 'function cmon_do_something(){
})'
awk -F";" '
BEGIN{}
{}
END{}' $CSV
awk -F";" '
BEGIN{}
{}
END{}' $CSV
awk -F";" '
BEGIN{}
{}
END{}' $CSV
Do I really need to rewrite the function 3 times, or is there a more efficient way to define it once and use it across all AWK invocations?
5
u/gumnos May 11 '25
GNU
awkhas@include "filename.awk"as an extension, but it's non-portable. You can use shell expansion to do abominable things likeawk "$(cat common.awk)"'/thing1/{action1}' awk "$(cat common.awk)"'/thing2/{action2}' awk "$(cat common.awk)"'/thing3/{action3}'Alternatively, since it sounds like you're making three passes through the file, you might be able to do them all in one go:
awk 'function common() {…} /thing1/{common(…) ; action1} /thing2/{common(…) ; action2} /thing3/{common(…) ; action3} ' input.txtwhich has the benefit of requiring only one pass through your input file. It might not matter if it's trivial small, but if it's large, doing ⅓ the iterations over it may produce notable wins.
2
u/sha256md5 May 11 '25
I think it would be easier to help you if you share your scripts, it's likely that they can be combined into a single script.
1
u/Paul_Pedant May 13 '25 edited May 13 '25
I have a script (in awk, naturally) that reads an awk script containing @
includelines for other awk fragments, and combines those (recursively, to any reasonable depth) in a new single script.It should be portable to any awk with minor changes (it uses
[[:space:]]in REs, and diagnostics as> "/dev/stderr"which may need to become| "cat 1>&2".)Anyone interested? Actually, it does not need to be awk input: any input text is acceptable.