redirecting system output

Discussion in 'Perl Misc' started by nahum_barnea@yahoo.com, Jul 23, 2006.

  1. Guest

    Hi.

    In my perl script I need to run a software that give output to standard
    output.
    I need to count the lines of that output.

    If I wanted to do it in csh I would do

    set a = `sw-tool .... | wc -l`


    But I wonna do it with perl and it seems there is no mechanism such as
    ` ... ` ?

    for example

    $a = `system "sw-tool | wc -l`;

    does not work properly.
    is there any clean perl solution except for using temporary file and
    reading it?

    thankX
    NAHUM
     
    , Jul 23, 2006
    #1
    1. Advertising

  2. John Bokma Guest

    wrote:

    > is there any clean perl solution except for using temporary file and
    > reading it?


    read from STDIN and count each line.

    perldoc -f open

    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Jul 23, 2006
    #2
    1. Advertising

  3. John Bokma Guest

    wrote:

    > Hi.
    >
    > In my perl script I need to run a software that give output to standard
    > output.
    > I need to count the lines of that output.
    >
    > If I wanted to do it in csh I would do
    >
    > set a = `sw-tool .... | wc -l`
    >
    >
    > But I wonna do it with perl and it seems there is no mechanism such as
    > ` ... ` ?
    >
    > for example
    >
    > $a = `system "sw-tool | wc -l`;
    >
    > does not work properly.
    > is there any clean perl solution except for using temporary file and
    > reading it?


    Addition: if you want to call sw-tool yourself, you open a pipe for
    reading to sw-tool (for example).

    What's wrong with the $a line:

    - bad variable name
    - did you use my $a (I guess not, did you use:

    use strict;
    use warnings;

    I guess not. If I am right, fix this)

    - You are calling:

    system "sw-tool | wc -l

    I doubt if that's what you want to do

    perldoc -f system


    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Jul 23, 2006
    #3
  4. wrote:
    >
    > In my perl script I need to run a software that give output to standard
    > output.
    > I need to count the lines of that output.
    >
    > If I wanted to do it in csh I would do
    >
    > set a = `sw-tool .... | wc -l`
    >
    >
    > But I wonna do it with perl and it seems there is no mechanism such as
    > ` ... ` ?
    >
    > for example
    >
    > $a = `system "sw-tool | wc -l`;
    >
    > does not work properly.
    > is there any clean perl solution except for using temporary file and
    > reading it?


    my $count_of_lines = () = `sw-tool ...`;

    Assuming that you haven't changed the value of $/.


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jul 23, 2006
    #4
  5. Xicheng Jia Guest

    wrote:
    > Hi.
    >
    > In my perl script I need to run a software that give output to standard
    > output.
    > I need to count the lines of that output.
    >
    > If I wanted to do it in csh I would do
    >
    > set a = `sw-tool .... | wc -l`
    >
    >
    > But I wonna do it with perl and it seems there is no mechanism such as
    > ` ... ` ?


    chomp( my $num_lines = `sw-tool .... | wc -l` );

    Xicheng
     
    Xicheng Jia, Jul 24, 2006
    #5
  6. <> wrote:
    >


    > If I wanted to do it in csh I would do
    >
    > set a = `sw-tool .... | wc -l`



    and if you wanted to do it in Perl you would do

    $a = `sw-tool .... | wc -l`;


    > $a = `system "sw-tool | wc -l`;

    ^^^^^^ ^
    ^ eh?

    system() is a _Perl_ function.

    What goes in the backticks are _shell_ commands.


    > does not work properly.



    GIGO.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 24, 2006
    #6
  7. Guest

    Thanks to all answers, and specially you Xicheng Jia,
    The "chomp" thing realy made my day :)

    Xicheng Jia wrote:
    > wrote:
    > > Hi.
    > >
    > > In my perl script I need to run a software that give output to standard
    > > output.
    > > I need to count the lines of that output.
    > >
    > > If I wanted to do it in csh I would do
    > >
    > > set a = `sw-tool .... | wc -l`
    > >
    > >
    > > But I wonna do it with perl and it seems there is no mechanism such as
    > > ` ... ` ?

    >
    > chomp( my $num_lines = `sw-tool .... | wc -l` );
    >
    > Xicheng
     
    , Jul 24, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Aspinall
    Replies:
    0
    Views:
    374
    Paul Aspinall
    Feb 26, 2006
  2. Birch
    Replies:
    3
    Views:
    1,045
    Birch
    Oct 21, 2003
  3. Eddie Parker

    RE: Redirecting system output quietly

    Eddie Parker, Nov 16, 2004, in forum: Python
    Replies:
    4
    Views:
    418
    Donn Cave
    Nov 16, 2004
  4. ash
    Replies:
    1
    Views:
    1,814
    Janusch
    Mar 25, 2008
  5. Replies:
    0
    Views:
    448
Loading...

Share This Page