using grep in Perl

K

Kimi

Hi,

I am trying to use grep mutiple values using grep command in my perl
script.

The command ---> ps -awwwx | grep 'Applications\|System' gives the
list of process which has Application OR System

When i try to use the similar expression in my Perl scripts, I am not
getting the expected output. Are the syntax different in perl? Can
somebody help me out in this case...

The corresponding line for grepping in my perl read as ---->

my $output = ` cat $accumulated_mail_body | grep
'Application\|System\|Corrupt block' | sort | uniq -c`;

regards,
Fahad
 
J

Jürgen Exner

Kimi said:
The command ---> ps -awwwx | grep 'Applications\|System' gives the
list of process which has Application OR System

When i try to use the similar expression in my Perl scripts, I am not
getting the expected output. Are the syntax different in perl?

Of coure it is. After all the language is Perl instead of ksh or csh or tcsh
or ...
The corresponding line for grepping in my perl read as ---->

my $output = ` cat $accumulated_mail_body | grep
'Application\|System\|Corrupt block' | sort | uniq -c`;

Why are you trying so hard to program shell in Perl?
- perldoc -f grep
- perldoc -f sort
- perldoc -q duplicate

Oh, yes, and as for your RegEx
- perldoc perlretut
but you are not using Perl's grep there, you know that, do you?

jue
 
J

John W. Krahn

Kimi said:
I am trying to use grep mutiple values using grep command in my perl
script.

The command ---> ps -awwwx | grep 'Applications\|System' gives the
list of process which has Application OR System

When i try to use the similar expression in my Perl scripts, I am not
getting the expected output. Are the syntax different in perl? Can
somebody help me out in this case...

The corresponding line for grepping in my perl read as ---->

my $output = ` cat $accumulated_mail_body | grep
'Application\|System\|Corrupt block' | sort | uniq -c`;

In Perl that would be written as:

open my $fh, '<', $accumulated_mail_body
or die "Cannot open '$accumulated_mail_body' $!";

my %uniq;
while ( <$fh> ) {
$uniq{ $_ }++ if /Application|System|Corrupt block/;
}
close $fh;

my $output = join '', map "$uniq{$_} $_", sort keys %uniq;




John
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top