awk - user function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - user function?
# 1  
Old 10-04-2013
awk - user function?

Here's a snippit from a script I'm working on

Code:
srvctl config database | \
awk -F\" '{print "srvctl config database -d " $1 " -a" \
         RS "echo =================================================="}' \
    > /tmp/x$$.sh
chmod 777 /tmp/x$$.sh
/tmp/x$$.sh

As you can see, I'm piping the output of' 'srvctl' to awk, where I construct other commands that get written to a file, then execute that file. It occurs that I should be able to eliminate that intermediate file and execute the commands directly from awk, but after struggling with some of the on-line tutorials as well as my trusty Unix in a Nutshell, I'm coming up dry. Here's my latest attempt ...

Code:
srvctl config database | \
awk -F\" 'function rptcfg(dbsid) {srvctl config database -d dbsid -a} \
          {print rptcfg($1) \
           RS "echo =================================================="}'

But it only produces a blank line for each of the (2) results from the 'srvctl' command:

Code:
echo ==================================================

echo ==================================================

# 2  
Old 10-04-2013
you may try with system function in awk.
# 3  
Old 10-04-2013
Quote:
Originally Posted by pamu
you may try with system function in awk.
Closer, but it appears that it is trying to do that in a secondary environment that does not inherit the first:

Code:
srvctl config database | \
awk -F\" '{system(srvctl config database -d $1 -a)} \
          {print  \
           RS "echo =================================================="}'

results in
Code:
sh: 00: command not found

echo ==================================================
sh: 00: command not found

echo ==================================================

# 4  
Old 10-04-2013
You might be able to just use xargs.

Something like:
Code:
srvctl config database | awk -F\" '{print $1}' | xargs -I%SUB% echo srvctl config database -d %SUB% -a

The echo is just so you can check if it works first.
# 5  
Old 10-04-2013
Quote:
Originally Posted by edstevens
Here's a snippit from a script I'm working on

Code:
srvctl config database | \
awk -F\" '{print "srvctl config database -d " $1 " -a" \
         RS "echo =================================================="}' \
    > /tmp/x$$.sh
chmod 777 /tmp/x$$.sh
/tmp/x$$.sh

As you can see, I'm piping the output of' 'srvctl' to awk, where I construct other commands that get written to a file, then execute that file. It occurs that I should be able to eliminate that intermediate file and execute the commands directly from awk, but after struggling with some of the on-line tutorials as well as my trusty Unix in a Nutshell, I'm coming up dry.

... ... ...
You're making this way to hard. If your script above works, the following should work just as well without using the temp file:
Code:
srvctl config database |
awk -F\" '{print "srvctl config database -d " $1 " -a" \
         RS "echo =================================================="}' |
sh

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 10-04-2013
Quote:
Originally Posted by CarloM
You might be able to just use xargs.

Something like:
Code:
srvctl config database | awk -F\" '{print $1}' | xargs -I%SUB% echo srvctl config database -d %SUB% -a

The echo is just so you can check if it works first.
Closer yet. Now how do I get my break line ("==================" back in?

---------- Post updated at 12:03 PM ---------- Previous update was at 12:01 PM ----------

Quote:
Originally Posted by Don Cragun
You're making this way to hard. If your script above works, the following should work just as well without using the temp file:
Code:
srvctl config database |
awk -F\" '{print "srvctl config database -d " $1 " -a" \
         RS "echo =================================================="}' |
sh

I like the elegance, but that has the same problem as the suggestion from Pamu ..
# 7  
Old 10-04-2013
So run your original script and show us the contents of /tmp/x$$.sh when it is working.

What operating system are you using. (I.e., what is the output from the command uname -a).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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.. (5 Replies)
Discussion started by: JSKOBS
5 Replies

2. 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

3. Shell Programming and Scripting

Function to silence rm -rf option for my user

Hi Gurus, I am trying to silence or supress rm -rf option for a particular user(venkat). for that am going to write a function in a script test_fun_ls.sh like below #!/bin/bash RM_FUNCTION () { if then case ${1} in -r) ... (9 Replies)
Discussion started by: venky.b5
9 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. 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

8. 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

9. 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

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
Login or Register to Ask a Question