using awk to drive an internal function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk to drive an internal function
# 1  
Old 09-22-2005
using awk to drive an internal function

I have a piece of script that needs to pull every element in column 2 from a file of varying size. Rather than writing something to read the file and then pass the data line by line to my script, I want to use awk.

However, what is the correct syntax to get awk to call a function (named via:

fcnname()
{
.
.
}
)

with the second column ($2), i.e. such as

awk '{fcnname($2)}' somefilename

I know I can do it from the command line or from yet another script and use the "system" command...and that is my next tack, but Id rather tie this all up in one pretty package.....

I have been playing with this syntax all day and not getting it right...

thanks
# 2  
Old 09-22-2005
Code:
awk ' function fcname(a) { print "col 2 =", a ; return 0 }
      { fcname($2) } ' < datafile

# 3  
Old 09-23-2005
Ok thanks for the response, ..I fear I might not have been clear on the problem.

consider the script 'foo' that looks like this:

#some comments
function f1(){}
function f2() {}

some option/parameter checking

if [single option]
then
f1 $parm1 $parm2
else #multiple option
awk '{ I want to call f1 with $2 and another parm $parm2 }' somefilename
fi

echo "all done"
exit 0

The above is what I meant by an internal function, it was internal to the program, not awk.

when I enclose f1 within the awk construct like thus: awk '{f1($2)}' some file name I get an error like this:

awk: line 0 (NR=1): variable "f1" cannot be used as a function

I should mention this is not on unix or linux per se but rather OMVS (aka Unix Systems Services) under z/OS on an IBM z/800, and seems to be a rather faithful implementation of every API thus far....
# 4  
Old 09-23-2005
You can't get there from here. When you define a shell function like that, the function is known only to that shell. Then you invoke awk which is a separate process. It has no way to peek inside its parent. At best, awk can spawn a child process of its own to access external commands. This child process cannot access a function known only to its grandparent.
# 5  
Old 09-23-2005
Thats what I was afraid of...oh well, thanks a bunch....
# 6  
Old 09-23-2005
yanno what.....something is nagging at me....

Right now I have a piece of script that looks like this: (anticipating your answer as given I started working at the problem from a different angle)

bar.sh:

# some comments

Some option checking

awk '{system("foo " $2 " " $parm2)}' somefile name

echo "done"

and I of course have foo.sh as before (minus the awk).

Are each of these 'system' calls a separate autonomous process? or merely a child? and will the execution of awk 'wait' until completion of each call?

the reason I ask is that parm '$2' names a unit of work that ABSOLUTELY MUST be executed in the order that it is listed in 'somefilename'.

{an example: 1) drive to store 2) buy stuff 3) drive home }

If we get out of order- disasterous results, mass hysteria, dogs and cats living together etc etc

I was trying to be slick but I might be forced to write a simple file reader to grab the second column item for the line....
# 7  
Old 09-23-2005
All processes are children and more or less autonomous. And yes system is spawning processes. There is no issue with stuff being done in the wrong order. More processes does mean more resources consumed. So the only issue is performance. The whole thing will run faster as a single process. And that would be really easy to do in ksh or bash...
Code:
while read col1 col2  AndTheRest ; do
         fcname $col2
done

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

Execute function as soon as input is provided in menu drive shell script

Hi All, I have a menu driven scripts. As you know while running the script we have to input the option such as 1,2, and 3 to execute function accordingly. but after selecting the input we have to press Enter. My requirement is to execute function as soon as we press the option. Is there... (5 Replies)
Discussion started by: kiran_j
5 Replies

3. Shell Programming and Scripting

using an awk internal variable as parameter for an external array

Hello, I am running a bash script under linux which first defines an CA-array like j=0 num1=120.00 num2=10.00 until do CA='echo $num1 + $j*$num2' j=$ done within the later awk section of this same script I want to read data from a file. If the value of the second column is... (3 Replies)
Discussion started by: MotAah
3 Replies

4. Shell Programming and Scripting

awk: Internal software error in the tostring function on

Hello, I posted a working script on this thread: https://www.unix.com/emergency-unix-linux-support-help-me/160123-help-make-awk-script-more-efficient-large-files.html When I run this script on a large file, 351 MB I get this error: awk: Internal software error in the tostring function on... (8 Replies)
Discussion started by: script_op2a
8 Replies

5. Shell Programming and Scripting

Internal software error in the tostring function for files greater than 300 MB

Hello, When I run my awk script with input files greater than 300 MB I always get this error: awk: Internal software error in the tostring function on TS1101?05044400?.0085498227?0?.0011041461?.0034752266?.00397045?0?0?0?0?0?0?11/02/10?09/23/10???10?no??0??no?sct_det3_10_20110516_143936.txt ... (0 Replies)
Discussion started by: script_op2a
0 Replies

6. Shell Programming and Scripting

use internal function with find command

Hi, I need to use a function in the find command to do some process on the file. I'm trying: funcname(){ ... } ... find ./ -name "*" -exec funcname {} \; But somehow this is not working. I don't want to have a separate script for whatever processing the function does. I want to have... (1 Reply)
Discussion started by: victorcheung
1 Replies

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

8. UNIX for Advanced & Expert Users

Forwarding internal internet packets to internal webserver using iptables

Hi, I need to redirect internal internet requests to a auth client site siting on the gateway. Currently users that are authenticated to access the internet have there mac address listed in the FORWARD chain. All other users need to be redirected to a internal site for authentication. Can... (1 Reply)
Discussion started by: mshindo
1 Replies

9. AIX

How to mount internal tape drive of sun machine in AIX 5.3

Hi, Help required! How to mount internal tape drive of sun machine in AIX 5.3 (0 Replies)
Discussion started by: ashwin.krishna
0 Replies

10. Shell Programming and Scripting

Help with the function awk

Hi I am trying to create a modify a txt file via a sh script and I'm not sure how to do it. I have this: data1a#data2a#data3aµ data1b#data2b#data3bµ data1c#data2c#data3cµ and I want to have this (more or less) data1a data2a data3a data1b data2b data3b data1c data2c data3c I know... (5 Replies)
Discussion started by: Morgwen
5 Replies
Login or Register to Ask a Question