Call user defined function from awk

My requirement is to call function ("fun1") from awk, and print its returned value along with $0.

fun1()
{
   t=$1
   printf "%02d\n", $t % 60;
}

echo "Hi There 23" | awk '{print $0; system(fun1 $3)}'

Any suggestions what to be modified in above code to achieve requirement..

Why not use awks' own printf? - Printf Examples (The GNU Awk User�s Guide)

We have existing function ("fun1") with complex calculations. For the sake of simplicity, i havn't mentioned here.
As part of reusability, we are asked to call this function wherever needed.

Your chances are low. While the function definition somehow survives - if export ed! - and makes it into awk 's environment under a modified name, like

BASH_FUNC_fun1%% = () {  t=$1;
 printf "%02d\n" $(($t % 60))
}

, the system command creates another process running /bin/sh which seems to lose the exported function definitions.

You can process your test with PHP or Python, for example, and then call your awk (or any external) function from there.

Hi JSKOBS...

Not quite exactly what you are looking for but a little bit of lateral thinking creates this.

#!/bin/sh
# awk_shell.sh

: > /tmp/myfunc
chmod 755 /tmp/myfunc

cat << "EOF" > /tmp/myfunc
#!/bin/sh
t=${1}
printf "%02d\n" "$(( t % 60 ))"
EOF

result=$( echo "Hi There 123" | awk '{ print $0; system("/tmp/myfunc "$3)}' )

echo "${result}"

Results; OSX 10.14.6, default bash terminal...

Last login: Wed Nov  6 20:47:08 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> chmod 755 awk_shell.sh
AMIGA:amiga~/Desktop/Code/Shell> ./awk_shell.sh
Hi There 123
03
AMIGA:amiga~/Desktop/Code/Shell> _
1 Like