Operating System - HP-UX
1825942 Members
2803 Online
109689 Solutions
New Discussion

awk's support of two-way communication with another process

 
Michael Gorman_1
Occasional Advisor

awk's support of two-way communication with another process

Hi!
Here is HP-UX B.11.00 computer with awk in /usr/bin/awk. Upon I tried to perform connection to other process by |&, got a message: "|& - cannot be correctly parsed". Does the gawk only can parse the operation? Or what can I do for this communication implementation?
Thanks.
How this works?
7 REPLIES 7
Ralph Grothe
Honored Contributor

Re: awk's support of two-way communication with another process

Hi Michael,

I'm not a strong supporter of awk, and thus only use it when one cannot expect Perl to be installed on a box.
So I don't know how awk can handle bidirectional communication.
But I do know that Perl can do this through pipe(), fork(), and exec().
Despite you should be carefull because bidirectional communication always has a latent peril of a deadlock situation, where either both sides are talking or listening simultaniously.
So you would have to make sure that only one side is either talking or listening.
If you want to do it in Perl have a look at the perlipc POD (i.e. at the shell type "perldoc perlipc"), which has many sample code fragments, and also addresses how to do bidirectional communication (so I think).
If you are interested, I could also send you chapter 16 of the "Perl Cookbook" (a must-have for any Perl hacker), which I have on CDROM, and is all about IPC and Perl.
Madness, thy name is system administration
Michael Gorman_1
Occasional Advisor

Re: awk's support of two-way communication with another process

Hi Ralph.
Thank you very much, but Perl is my next target and now I have to do what I can. Anyway, your suggestions are welcome.
Thanks.
How this works?
James Beamish-White
Trusted Contributor

Re: awk's support of two-way communication with another process

Can you post your full command line so we can look at it?

Cheers!
James
GARDENOFEDEN> create light
Michael Gorman_1
Occasional Advisor

Re: awk's support of two-way communication with another process

file copr.awk
#
# test coprocessing
#
BEGIN {
command = "LC_ALL=C sort"
n = split("abcdefghijklmnopqrstuvwxyz", a, "")

for (i = n; i > 0; i--)
print a[i] |& command
close(command, "to")

while ((command |& getline line) > 0)
print "got", line
close(command)
}
#####eof

/usr/bin/awk -f copr.awk
syntax error The source line is 12.
The error context is
print a[i] >>> |& <<< command
awk: The statement cannot be correctly parsed.
The source line is 12.
syntax error The source line is 14.
How this works?
Rodney Hills
Honored Contributor

Re: awk's support of two-way communication with another process

man page on "awk" doesn't mention "|&". The "ksh" does work with "|&" to generate co-processes.

Are you sure that awk supports "|&"?

-- Rod Hills
There be dragons...
Michael Gorman_1
Occasional Advisor

Re: awk's support of two-way communication with another process

Rodney, you're right, but as I mentioned it looks like awk does not support this, but gawk does. Is it right I have to install gawk or there're some other tricks?
Thanks.
How this works?
Rodney Hills
Honored Contributor

Re: awk's support of two-way communication with another process

As Ralph mentioned above, I think you might be better served directing your efforts to developing this with perl. Before perl, I too use to try to get awk to do some compliciated things, but the code looked pretty ugly and their were no debugging tools.

If you really want to make this work with awk, then you can get gawk from a CPAN site like http://hpux.cs.utah.edu/.

Good Luck

-- Rod Hills
There be dragons...