awk - coprocess???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - coprocess???
# 1  
Old 03-01-2007
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.

Code:
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

# 2  
Old 03-01-2007
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).
Code:
#! /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

# 3  
Old 03-01-2007
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??
# 4  
Old 03-01-2007
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.
# 5  
Old 03-01-2007
Quote:
Originally Posted by Perderabo
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.
Ok, I would use them with them from here onwards - Thanks again for letting me know about the coprocess stuff!!!!
# 6  
Old 07-10-2007
Quote:
Originally Posted by Perderabo
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.
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:
Code:
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

Code:
./cp.sh |&

print -p 123

read -p VAR ; print $VAR
1

...which works well enough for easy text parsing.
# 7  
Old 07-11-2007
with GNU awk, you can use "|&" to do coprocess
eg
Code:
     print "sql query" |& "database server"
     "database server" |& getline

see GNU awk docs for details
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

5. Shell Programming and Scripting

SQL/Plus in a coprocess example. Also saves query results into shell variables

While assisting a forum member, I recommended running SQL/Plus in a coprocess (to make database connections and run a test script) for the duration of his script rather than starting/stopping it once for every row in a file he was processing. I recalled I made a coprocess example for folks at... (2 Replies)
Discussion started by: gary_w
2 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

KSH, coprocess and SSH

Hi there, I want to connect to a Cisco router with a KSH script via coprocess: telnet 192.168.2.82|& print -p “login” print -p "password" With telnet it works. Now I want to use SSH: ssh -T -l login 192.168.2.82|& print -p "password" The router answer me I enter a bad... (7 Replies)
Discussion started by: sylvainkalache
7 Replies

8. Shell Programming and Scripting

Get coprocess output into var

This is probably a simple one for the wise. I have just started using a coprocess (first time) in order to facilitate telnet'ing from inside a shell script. It's working, but when I run a remote command I need to get the output into a local variable, but alas my kung-fu is weak. #!... (10 Replies)
Discussion started by: dan-e
10 Replies

9. Shell Programming and Scripting

Korn Shell Coprocess Performance Question

I am wracking my brains over this. I am trying to use a Korn Shell script to execute an Oracle PL/SQL procedure, using the Oracle command line interface (sqlplus). The script starts sqlplus in a coprocess, and the two processes communicate using a two-way pipe. The bgnice option is off, so both... (8 Replies)
Discussion started by: Mark Puddephat
8 Replies

10. Shell Programming and Scripting

I/O redirection within a coprocess

Hello everybody, I have a question about I/O redirection within a coprocess. I want to setup a coprocess and then redirect output to a file on a remote machine. Here's some Perderabo code modified exec 4>&1 # # Section 1 --- Prove that we can talk with the hosts in HOSTLIST # ... (4 Replies)
Discussion started by: Mugin
4 Replies
Login or Register to Ask a Question