![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| regarding internal modems | rajas1982 | Windows & DOS: Issues & Discussions | 3 | 12-30-2007 09:14 PM |
| How to mount internal tape drive of sun machine in AIX 5.3 | ashwin.krishna | AIX | 0 | 11-15-2007 06:38 AM |
| Unix Internal | Rengi | High Level Programming | 3 | 06-27-2005 06:54 AM |
| internal routing & vpn | wachichornia | IP Networking | 0 | 03-08-2005 01:58 PM |
| internal security | ust | UNIX for Advanced & Expert Users | 2 | 02-17-2005 05:35 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Code:
awk ' function fcname(a) { print "col 2 =", a ; return 0 }
{ fcname($2) } ' < datafile
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Thats what I was afraid of...oh well, thanks a bunch....
|
|
#6
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
| Google The UNIX and Linux Forums |