system problem

C

Chip

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
 
G

Gunnar Hjalmarsson

Chip said:
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
 
A

A. Sinan Unur

Chip said:
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
 
C

Chip

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
 
J

Joe Smith

Chip said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top