system problem

Discussion in 'Perl Misc' started by Chip, May 17, 2005.

  1. Chip

    Chip Guest

    Thanks in advance for your help.

    I am trying to fill an array by using grep
    so I can print out only the lines with ip
    addresses in them.

    The following prints the system call on
    the screen but does not fill the array.

    I don't want the system call to print on
    the screen, and I want to fill the array.

    #!/usr/bin/perl -w
    use strict;
    my @array = system("ps ax | grep sendmail");
    foreach (@array) {
    if ($_ =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
    print "$_ \n";
    }
    }

    Any and all help is appreciated.

    Chip
     
    Chip, May 17, 2005
    #1
    1. Advertising

  2. Chip wrote:
    > The following prints the system call on
    > the screen but does not fill the array.
    >
    > I don't want the system call to print on
    > the screen, and I want to fill the array.
    >
    > #!/usr/bin/perl -w
    > use strict;
    > my @array = system("ps ax | grep sendmail");


    Then don't use system(). The docs explain why and what to use instead:

    perldoc -f system

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 17, 2005
    #2
    1. Advertising

  3. "Chip" <> wrote in news::

    > The following prints the system call on
    > the screen but does not fill the array.
    >
    > I don't want the system call to print on
    > the screen, and I want to fill the array.


    Then you should read the documentation:

    perldoc -f system

    > #!/usr/bin/perl -w


    use warnings;

    is preferable. Read more about it in

    perldoc warnings
    perldoc perllexwarn

    > use strict;
    > my @array = system("ps ax | grep sendmail");


    Once you have read about the backtick operator (see above),
    you might still want to avoid spawning the grep above, and
    instead use a pipe open to ps, and process the output of the
    command line by line. See:

    perldoc -f open

    > foreach (@array) {
    > if ($_ =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {


    That would match things other than IP addresses as well.

    See

    <URL: http://search.cpan.org/dist/Regexp-Common/lib/Regexp/Common/net.pm>

    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, May 17, 2005
    #3
  4. Chip

    Chip Guest

    Thanks for your help.

    #!/usr/bin/perl
    use strict;
    use warnings;
    my $ps = `ps ax | grep sendmail`;
    my @array = split /\n/, $ps;
    foreach (@array) {
    if ($_ =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
    print "$_ \n";
    }
    }

    Seems to work ok for me.
    Chip

    "A. Sinan Unur" <> wrote in message
    news:Xns9658C88D4120Dasu1cornelledu@127.0.0.1...
    > "Chip" <> wrote in

    news::
    >
    > > The following prints the system call on
    > > the screen but does not fill the array.
    > >
    > > I don't want the system call to print on
    > > the screen, and I want to fill the array.

    >
    > Then you should read the documentation:
    >
    > perldoc -f system
    >
    > > #!/usr/bin/perl -w

    >
    > use warnings;
    >
    > is preferable. Read more about it in
    >
    > perldoc warnings
    > perldoc perllexwarn
    >
    > > use strict;
    > > my @array = system("ps ax | grep sendmail");

    >
    > Once you have read about the backtick operator (see above),
    > you might still want to avoid spawning the grep above, and
    > instead use a pipe open to ps, and process the output of the
    > command line by line. See:
    >
    > perldoc -f open
    >
    > > foreach (@array) {
    > > if ($_ =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {

    >
    > That would match things other than IP addresses as well.
    >
    > See
    >
    > <URL: http://search.cpan.org/dist/Regexp-Common/lib/Regexp/Common/net.pm>
    >
    > 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
     
    Chip, May 17, 2005
    #4
  5. Chip

    Joe Smith Guest

    Chip wrote:

    > my $ps = `ps ax | grep sendmail`;
    > my @array = split /\n/, $ps;


    Change that to either

    my @array = `ps ax | grep sendmail`;

    or

    my @array = grep /sendmail/,`ps ax`;

    -Joe
     
    Joe Smith, May 17, 2005
    #5
    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. sfunds sfunds
    Replies:
    9
    Views:
    8,730
  2. Charles A. Lackman

    Streaming with System.IO and System.Net

    Charles A. Lackman, Dec 8, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    548
    Richard Grimes [MVP]
    Jan 15, 2005
  3. Mark Miller
    Replies:
    1
    Views:
    1,087
    Mark Miller
    Jul 18, 2003
  4. Matt
    Replies:
    2
    Views:
    842
    Matt Getting
    Jul 24, 2003
  5. Brad
    Replies:
    2
    Views:
    10,239
Loading...

Share This Page