tags:

views:

77

answers:

3

ps efax 2>/dev/null|grep firstbo|grep -v grep|wc -l

if i store this as a scalar, then, the scalar contains a new line, how do i remove the new line

+3  A: 

chomp $scalar will eat the newline

Konerak
+6  A: 
chomp $scalar;

But everything after the ps can be done more efficiently inside your script, like this:

my $count = 0;
open(my $ps, "ps -e -o comm |") or die "failed to spawn ps: $!";
while(<$ps>) {
    $count++ if /firstbo/;
}
close $ps;
Zack
`open my $ps, ...` -- avoid globals as much as possible.
Ether
True 'dat, I've edited my code. I learned Perl back in the days when you *had* to use barewords for filehandles, and haven't used it seriously in years.
Zack
The only problem is that this command runs on a remote host, using Net::SSH::Expect
kamal
Ah, so you want to minimize data transferred over the network? In that case, try `ps -e -o comm | awk 'BEGIN{count=0}/[f]irstbo/{count++}END{print count}'` and you'll still need to `chomp` your scalar.
Zack
Or `ps -e -o comm | grep -c '[f]irstbo'` will be even more efficient, if you have GNU coreutils on the remote host.
Zack
so complicated... `scalar(@{[qx(ps -efa)]});`
Dummy00001
I don't get what the `@{[ ... ]}` part does, but `ps -efa` is not at all the same as `ps -e -o comm | grep [f]irstbo`, dude.
Zack
+2  A: 

Use the chomp operator. You can also condense your command by taking advantage of the fact that the re in grep stands for regular expression:

chomp(my $num_firstbo = `ps efax 2>/dev/null | grep [f]irstbo | wc -l`);

By matching against a singleton character class, the command above matches processes whose argvs contain firstbo, but not the grep command itself.

Greg Bacon