external function in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting external function in awk
# 1  
Old 01-10-2008
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 !
# 2  
Old 01-10-2008
Awk also supports user defined functions, I should read some awk manuals.

Regards
# 3  
Old 01-10-2008
awk is not shell, can i call the function through the callers system()
function??

regards
# 4  
Old 01-10-2008
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.
# 5  
Old 01-10-2008
Hi.

Here is a script that attempts various methods of calling functions:
Code:
#!/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:
Code:
% ./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
# 6  
Old 01-10-2008
Excellent post drl. Thx
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

3. Shell Programming and Scripting

Passing external variable to awk

Hi, I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value. Here is my sample code. #!/usr/bin/bash ###################################################################################### ####... (5 Replies)
Discussion started by: jpkumar10
5 Replies

4. Shell Programming and Scripting

Problem in passing date to external function from perl script.

my $sysdate = strftime('%Y-%m-%d', localtime ); biDeriveByDate('Table_Str',$sysdate,\@lIndx,\@lResVals) In a perl script, when I'm trying to pass $sysdate to some external function it's not working since $sysdate is passed as a string mentioned above but my function is expecting a date value... (1 Reply)
Discussion started by: Devesh5683
1 Replies

5. Shell Programming and Scripting

Help needed with awk external variable

I'm trying to get the universities result data into different file, where the $9 contains unversity field and field7,4 & 5 contains the keys to sort the students by marks. How to use uni variable to match against $9 inside awk? c=0 for uni in `cat /tmp/global_rank| awk -F ',' '{print... (1 Reply)
Discussion started by: InduInduIndu
1 Replies

6. UNIX for Dummies Questions & Answers

Call external function

Is it possible to call a function from another script? Thanks (6 Replies)
Discussion started by: chrisjones
6 Replies

7. Shell Programming and Scripting

How to Call external function in .C or .So (Shared Object)

Hi, Anybody know any way to Call with Shell Script an external function wrote in .C or .So (Shared Object) on AIX enviroment and returning parameters of .C or .SO to Shell Script? Tks!! (6 Replies)
Discussion started by: rdgsantos
6 Replies

8. Shell Programming and Scripting

Can you ref/link/import a function from external ksh script?

Hey guys, I'm not the best AIX scripter about but I can flounder my way thru them to create what I need. Anyhow, I have various scripts that perform various actions and processes. I was tasked to come up with a single form of logging that all the scripts could implement so that the output... (2 Replies)
Discussion started by: isawme
2 Replies

9. Shell Programming and Scripting

Calling external function in a shell

hi guys, how r u??? please I need you, help me please. I have a shell, in this shell i have this function and another code lines, this function is getting date one day back. the function is in the same shell (FILE 1) Now I need put this function in another file (FILE 2) and calling... (4 Replies)
Discussion started by: acevallo
4 Replies

10. Shell Programming and Scripting

awk help (external variable)

i need help with an awk question. i am looking to have an external variable be defined outside of awk but used in awk. so if we have fields $1, $2, $3 so on and so forth, i would like to be able to dictate what field is being printed by something like $. so if i had a counter called test, make it 3... (8 Replies)
Discussion started by: pupp
8 Replies
Login or Register to Ask a Question