Call user defined function from awk


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Call user defined function from awk
# 1  
Call user defined function from awk

My requirement is to call function ("fun1") from awk, and print its returned value along with $0.
Code:
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..
# 2  
Why not use awks' own printf? - Printf Examples (The GNU Awk User’s Guide)
# 3  
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.
# 4  
Your chances are low. While the function definition somehow survives - if exported! - and makes it into awk's environment under a modified name, like
Code:
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.
# 5  
You can process your test with PHP or Python, for example, and then call your awk (or any external) function from there.
# 6  
Hi JSKOBS...

Not quite exactly what you are looking for but a little bit of lateral thinking creates this.
Code:
#!/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...
Code:
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> _

This User Gave Thanks to wisecracker For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #270
Difficulty: Easy
Steve Jobs died of respiratory arrest related to a car accident at age 56 on October 5, 2011.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call function as different user within the script

Hello For HP-UX, ksh shell, is it possible to define functions and call them by another user ? For example <function_name> ( ) { command1 command2 } su - <user> -c <function_name> Or the only option is defining the user in the function itself as follows - <function_name> ( )... (2 Replies)
Discussion started by: atanubanerji
2 Replies

2. UNIX for Dummies Questions & Answers

Problem syntax with user-defined function

Hi ! I got a script from Arabic to Roman numeral conversion - .comp.lang.awk, that I would like to modify to apply it on my input file. input ("|"-delimited fields): AAAAAA|1, 10, 13, 14, 25, 60 wanted output: AAAAAA|I, X, XIII, XIV, XXV, LX script.awk: #!/usr/bin/gawk -f ... (11 Replies)
Discussion started by: lucasvs
11 Replies

3. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

4. Shell Programming and Scripting

How to pass parameter to User defined function in shell script?

Hello, Can anyone guide me tin passing parameters into user defined function of shell script (KSH). Here is my code, InsertRecord() { DB_TBL=$(sqlplus $USERID/$PASSWORD@$DATABASE << EOF set head off set feed off set serveroutput on INSERT INTO TBL1 ( OLD_VAL, NEW_VAL, ... (7 Replies)
Discussion started by: Poonamol
7 Replies

5. UNIX and Linux Applications

strange behavior of PSQL user defined function

Segregated the problematic portion, and showing for your view here., 1. Following is the function definition, create or replace function new_del(id integer) returns void as $$ begin raise info 'dollar :%',$1; delete from testing where id=$1; end ; $$ language 'plpgsql'; ... (1 Reply)
Discussion started by: thegeek
1 Replies

6. Shell Programming and Scripting

Call Function in .pm as another user

Hello, I need to call a function which reside in some package moudle (.pm) as another user by using su -. Are anyone know how can I do it? Thanks (1 Reply)
Discussion started by: Alalush
1 Replies

7. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 Replies

8. Shell Programming and Scripting

need help with User Defined Function

Dear Friends, I need a help regarding User defined function in shell script. My problem is as follows: my_func.sh my_funcI(){ grep 'mystring' I.dat } my_funcQ(){ grep 'mystring' Q.dat } myfuncI myfuncQ But As both the function has same function only the... (11 Replies)
Discussion started by: user_prady
11 Replies

9. Shell Programming and Scripting

awk printf for user defined variables

I am working on a SunFire 480 - uname -a gives: SunOS bsmdb02 5.9 Generic_112233-08 sun4u sparc SUNW,Sun-Fire-480R I am tyring to sum up the total size of all the directories for each running database using awk: #!/usr/bin/ksh for Database in `ps -efl | grep "ora_pmon" | grep -v grep |... (1 Reply)
Discussion started by: jabberwocky
1 Replies

10. Shell Programming and Scripting

Nawk user-defined function

HELP!!!! I am in an on-line shell programming class and have a question. Here is the data: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 (There are 12 contribuors total) This database contains names, phone... (1 Reply)
Discussion started by: NewbieGirl
1 Replies

Featured Tech Videos