piping command output to open() 
Author Message
 piping command output to open()


:
:Hi,
:
:I need to be able to pipe the output of a command to open().  I understand
:how to do this, but is it possible if the command itself contains multiple
:pipes?
:
:Example:
:
:open (MAXNUM, "cat testfile | awk -F: '{print $3}' |");
:
:I want take each line in testfile, pull the third token (delimited by :),
:and have Perl process it.  When I do this though, Perl stops
:execution on the command after the first pipe.  So all it does is
:'cat testfile', but it doesn't parse out the third token with awk.
:I know I can parse it out inside Perl, but out of curiousity, I'd
:like to know if there's a way to do it like this.
:
:Is there a way around this?  I don't want to have to redirect this
:output to any intermediate file.
:
:Environment:  Perl 5.000 on Solaris 2.3
:
:thanks for any pointers,

(I bet references would be even better.)

The problem is $3 is interpolating, leaving awk with merely {print}.

You mean:

    open (MAXNUM, "awk -F: '{print \$3}' testfile |")
        || die "cannot fork awk: $!";

or

    open (MAXNUM, q{awk -F: '{print $3}' testfile |})
        || die "cannot fork awk: $!";

There's (nearly) never any need to cat just one file at something, btw.

For the record, doing it all in perl would be:

    open (MAXNUM, "testfile")         || die "cannot open tesfile: $!";
    while (<MAXNUM>) {
        print +(split(/:/))[3], "\n";
    }

That silly plus is to inhibit perl from thinking I'd said:

        (print split(/:/)) [3], "\n";

which wuldn't have worked well at all.

--tom
--

    There is, however, a strange, musty smell in the air that reminds me of
    something...hmm...yes...I've got it...there's a VMS nearby, or I'm a Blit.
        --Larry Wall in Configure from the perl distribution



Mon, 28 Apr 1997 20:29:38 GMT  
 piping command output to open()

Hi,

I need to be able to pipe the output of a command to open().  I understand
how to do this, but is it possible if the command itself contains multiple
pipes?

Example:

open (MAXNUM, "cat testfile | awk -F: '{print $3}' |");

I want take each line in testfile, pull the third token (delimited by :),
and have Perl process it.  When I do this though, Perl stops
execution on the command after the first pipe.  So all it does is
'cat testfile', but it doesn't parse out the third token with awk.
I know I can parse it out inside Perl, but out of curiousity, I'd
like to know if there's a way to do it like this.

Is there a way around this?  I don't want to have to redirect this
output to any intermediate file.

Environment:  Perl 5.000 on Solaris 2.3

thanks for any pointers,

tim ma
DIGIT Lab, university of utah



Mon, 28 Apr 1997 06:12:33 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. opening pipe for input and output

2. close error when open is a pipe with no output (and a -w question)

3. Opening a pipe to a command

4. open a piped command including stderr

5. capturing output of piped commands

6. open COMMAND with a pipe

7. open command that pipes both in and out

8. Help with Piped command, capturing output

9. Pipe the output of a command to script

10. unix command displayed when using PIPES with open.

11. Killing a pipe-opened command in Win32?

12. help with capturing output of open command

 

 
Powered by phpBB® Forum Software