external function in awk

Hi all, I have a basic doubt. Is there any way to use external
functions (i.e. functions not defined in AWK), in AWK.

I have a shell script in which I'm using a AWK snippet. In this
snippet I'm calling a function defined in the shell script. But the AWK
snippet is not working. I figured that this problem was due to the use
of external function in AWK.

Is there any way to solve this tell me how i can do it .

Please Help !

Awk also supports user defined functions, I should read some awk manuals.

Regards

awk is not shell, can i call the function through the callers system()
function??

regards

You can call shell scripts from awk using the awk system function. system(expr) uses /bin/sh to execute expr and returns the exit code.

You cannot call individual used-defined shell functions using this method (general case, though somebody could prove me wrong.) You could put the user-defined shell function in a separate script and call that script using the awk system function.

Hi.

Here is a script that attempts various methods of calling functions:

#!/usr/bin/env sh

# @(#) s1       Demonstrate functions with awk.

#  ____
# /
# |   Infrastructure BEGIN

set -o nounset
echo

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash awk

my_shell_local()
{
  echo " Local function $FUNCNAME called."
}

kamel_seg()
{
  echo " Function $FUNCNAME called with arguments \"$*\"."
}

echo
echo " Function is exported to sub-shells:"
export -f kamel_seg
typeset -F

# |   Infrastructure END
# \
#  ---

echo
echo " Calling shell function from shell:"
kamel_seg hello

echo
echo " Calling shell function from awk:"
awk '
BEGIN { print " In awk" ; kamel_seg }
'

echo
echo " Calling shell function from awk system() call:"
awk '
BEGIN { print "In awk - system" ;
 system(" echo Hi from system call" ) ; system( "kamel_seg from awk" ) }
'

echo
echo " Calling awk function:"
awk '
function my_function( message )
{
  print " awk function my_function called, argument:" message
}
BEGIN { print " Calling my_function from BEGIN block."
 my_function( " Hello there." )
}
'

echo
echo " Calling awk program that calls a shell script with system():"
awk '
BEGIN { print " Calling helper." ; system( "./helper" ) }
'

exit 0

Producing:

% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
GNU Awk 3.1.4

 Function is exported to sub-shells:
declare -fx kamel_seg
declare -f my_shell_local

 Calling shell function from shell:
 Function kamel_seg called with arguments "hello".

 Calling shell function from awk:
 In awk

 Calling shell function from awk system() call:
In awk - system
Hi from system call
 Function kamel_seg called with arguments "from awk".

 Calling awk function:
 Calling my_function from BEGIN block.
 awk function my_function called, argument: Hello there.

 Calling awk program that calls a shell script with system():
 Calling helper.
 Script "helper" called.
 awk script called from shell script helper

Among the other points, this illustrates that shell functions can be called from awk system(), provided the shell functions are exported (frankly, I was not expecting this; I simply was trying to exhaust all the possibilities).

The brief declare output shows the difference between local and exported functions.

Best wishes ... cheers, drl

Excellent post drl. Thx