How can I send the output of the system() command to a file and capture a string in that file

Discussion in 'Perl Misc' started by prashant_ec2003@yahoo.com, Jun 19, 2005.

  1. Guest

    Hello All,

    I am newbie .
    waiting for your suggstions and guidance.

    My requirement : I need to send the output of the system() command to a
    file , then extract a string from one of the lines in that file .How
    can i do this.

    Thanks in advance
    Prashant
     
    , Jun 19, 2005
    #1
    1. Advertising

  2. wrote in news:1119209407.636013.158950
    @z14g2000cwz.googlegroups.com:

    > I am newbie .
    > waiting for your suggstions and guidance.


    Instead of waiting, you should utilize your time by reading the posting
    guidelines for this group to learn how you can help yourself, and help
    others help you.

    > My requirement : I need to send the output of the system() command
    > to a


    perldoc -f system

    > file , then extract a string from one of the lines in that file .How
    > can i do this.


    perldoc perlretut

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 19, 2005
    #2
    1. Advertising

  3. wrote:
    > Hello All,
    >
    > I am newbie .
    > waiting for your suggstions and guidance.
    >
    > My requirement : I need to send the output of the system() command to
    > a file ,


    Well, strictly speaking system() itself doesn't generate any output. To send
    the output of the external command to a file just use the output redirect
    command of your command shell to redirect the output to a file. Which symbol
    that is depends on the command shell you are using, but very often a ">"
    will work.

    > then extract a string from one of the lines in that file .How
    > can i do this.


    You open() the file, read it line by line, and when you found the right line
    exctact the string. Sorry, more detailed advice is not possible because you
    didn't give us any details, either.

    However, using system(), redirect the command output to a file, then reading
    and scanning the file for a string is normally the wrong way to go.
    Unless you need the external file for other reasons you will be way better
    of using backticks or qx// to call the external program because those
    commands will capture the command output and return it (details see "perldoc
    perlop, section "strings"). Then all you have to do is to grep() the correct
    line from the returned array and extract the string.

    jue
     
    Jürgen Exner, Jun 19, 2005
    #3
  4. Guest

    Re: How can I send the output of the system() command to a file and capture a string in that file

    can i include the system() command in the open() commnad

    example open ( FH , "system ("...") | ) and the like . it may be
    incorrect but correct me if i am wrong..with the symbols used ..

    This is the where i am finding difficulty .Please help
     
    , Jun 19, 2005
    #4
  5. Re: How can I send the output of the system() command to a file and capture a string in that file

    wrote in news:1119212936.883572.121130
    @f14g2000cwb.googlegroups.com:

    > can i include the system() command in the open() commnad
    >
    > example open ( FH , "system ("...") | )


    You cannot do that. Have you read

    perldoc -f system

    yet?

    system on returns information on whether the call succeeded, not the
    output of the command executed.

    You can, on the other hand, open a pipe to the program. For example:

    #! /usr/bin/perl

    use strict;
    use warnings;

    open my $ls_pipe, "-|", 'ls -l'
    or die "Cannot open pipe to read ls output: $!";

    print "Read: $_" while <$ls_pipe>;

    close $ls_pipe
    or die "Error closing pipe to ls: $!";

    __END__

    > it may be incorrect but correct me if i am wrong ...


    You did not make a statement, so you cannot be wrong.

    > with the symbols used ..


    I don't know what that means.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 19, 2005
    #5
  6. Big and Blue Guest

    Re: How can I send the output of the system() command to a file andcapture a string in that file

    wrote:
    > can i include the system() command in the open() commnad
    >
    > example open ( FH , "system ("...") | ) and the like . it may be
    > incorrect but correct me if i am wrong..with the symbols used ..


    Try reading the documentation for "open".

    Also, you say you need to write a file, then get Perl to read it. Is
    that the actually requirement? Do you just in fact want Perl to be able to
    extract a value from the output of an external command? And you assume
    that using a file for this is the way to go?

    If that is the case thne your assumptions are wrong (and try not to
    post assumptions as requirements).

    You can "open" an external command and read its outpur directly.


    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
     
    Big and Blue, Jun 20, 2005
    #6
    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. Ernesto
    Replies:
    1
    Views:
    795
    Ernesto
    Feb 1, 2006
  2. Peter A. Schott
    Replies:
    4
    Views:
    603
    Peter A. Schott
    Feb 7, 2006
  3. Damjan Rems
    Replies:
    7
    Views:
    294
    Albert Schlef
    Apr 12, 2010
  4. Dib Urim
    Replies:
    4
    Views:
    209
    Ben Liddicott
    Nov 23, 2003
  5. Replies:
    8
    Views:
    178
Loading...

Share This Page