awk - coprocess???

Hi can any one let me know if awk doesnt work with the coprocess??? I have tried a simple example mentioned below but couldnt get it working seems like awk doesnt work with the coprocess concept. I would appreciate very much for any inputs on this.

exec 4>&1
awk -v count=$COUNT >&4 2>&4  |&
print -p  '{
print -p  print count
echo $COUNT
print -p }' xxxx  > ~/result
wait
exit 0

awk does not work as a coprocess for me. It hangs when I try to read a result and it probably is buffering the input. But you are redirecting the output of the coprocess back to standard-in anyway. I don't understand what you're trying to do with that script. Here is a script that uses a bc coprocess to add numbers (which works) and tries to uses an awk coprocess to add numbers (which hangs).

#! /usr/bin/ksh
bc |&
bcpid=$!

print -p 1 + 2
read -p result
echo bc says 1 + 2 = $result

print -p 1.75  + 3.25
read -p result
echo bc says 1.75 + 3.25 = $result

kill $bcpid
wait

set -x
awk '{print $1+$2}' |&
awkpid=$!

print -p 1 2
read -p result
echo awk says 1 + 2 = $result

print -p 1.75 3.25
read -p result
echo awk says 1.75 + 3.25 = $result

kill $awkpid
wait

exit 0

Well, I was trying to exchange the values between the standard-input and the awk. so, is there a way to find which are all the list of commands that work as a co process??? Wouldn't it be possible to have the coprocess within awk??

Only way is to try the command and see. There aren't that many commands that would be useful as coprocesses. I regularly use adb, bc, ftp, and telnet. These are commands that were intended to be used interactively... a strong clue that they are candidates for a coprocess.

Ok, I would use them with them from here onwards - Thanks again for letting me know about the coprocess stuff!!!!

Haven't logged in for a while and I stumbled on this post.

I could never get awk to work as a coprocessor either. I was mentoring a coworker on coprocessors and while he was testing the fundamentals, I noticed that he wrapped awk in a shell script something like this:

Script cp.sh

#! /usr/bin/ksh -p

while :
do
    read LINE
    [[ "$LINE" = "QUIT" ]] && break

    print - "$LINE" | nawk '{
        if ( $0 ~ /^[0-9][0-9][0-9]/ ) {
            print "1"
        } else {
            print "0"
        }
    }'
done
./cp.sh |&

print -p 123

read -p VAR ; print $VAR
1

...which works well enough for easy text parsing.

with GNU awk, you can use "|&" to do coprocess
eg

     print "sql query" |& "database server"
     "database server" |& getline

see GNU awk docs for details