1829028 Members
2519 Online
109986 Solutions
New Discussion

grep function in perl

 
SOLVED
Go to solution
Mike_Ca Li
Regular Advisor

grep function in perl

How to incorporate "grep -w $variable" in perl grep? eg:
@ps = grep(/$variable*/, @ps);
Thanks
20 REPLIES 20
Ralph Grothe
Honored Contributor
Solution

Re: grep function in perl

Use the \b anchor which denote word boundaries in Perl regexps.
Place your match (i.e. $variable) within these.
See also "perldoc perlre"
Madness, thy name is system administration
H.Merijn Brand (procura
Honored Contributor

Re: grep function in perl

@ps = grep /\b$variable\b/ => @ps;

Enjoy, Have FUN! H.Merijn [ a more detailed question could have caused a more verbose answer ]
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: grep function in perl

An image says more than thousand words it says.
So procura has given you the image of my clumsy description.
However, don't let yourself get confused by his style.
The => is, as they say, syntactic sugar the professinals use to discern themselves from us mortal Perl foot folk.
grep really takes a list as argument.
see "perldoc -f grep"
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: grep function in perl

Ah something else I realized.
You are reading and assigning to the array @ps at the same time.
Beware the this will only substitute the leading array elements that produced a match.
The tail holds still the old array elements.
So this may not exactly be what you expect,
especially if you rely on the array's dimension (e.g. in scalar context)
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: grep function in perl

Ah something else I realized.
You are reading and assigning to the array @ps at the same time.
Beware that this will only substitute the leading array elements that produced a match.
The tail holds still the old array elements.
So this may not exactly be what you expect,
especially if you rely on the array's dimension (e.g. in scalar context)
Madness, thy name is system administration
Mike_Ca Li
Regular Advisor

Re: grep function in perl

Thanks for replies.
where to find detailed documentation of \b$variable\b.

perldoc -f grep is very short.
Ralph Grothe
Honored Contributor

Re: grep function in perl

I told you already, read "perldoc perlre"
Madness, thy name is system administration
H.Merijn Brand (procura
Honored Contributor

Re: grep function in perl

Ralph, no!

@ps = .... @ps

grep / map / function / whatever

*REPLACES*

@ps completely. No tail or head will remain.

BTW If @ps refers to a process list (e.g. from `ps`), I'd suggest having a look at Proc::ProcessTable

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: grep function in perl

Sorry for spreading lies.

I just verified in the debugger,
procura's right.


Madness, thy name is system administration
H.Merijn Brand (procura
Honored Contributor

Re: grep function in perl

No debugger needed. An image again :)

lt09:/home/merijn 104 > perl -le'$,=" ";@a=(1..20);print@a;@a=grep/7/,@a;print@a'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7 17
lt09:/home/merijn 105 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: grep function in perl

For geeks like you that's ok.
But for me it would take longer to fumble up the oneliner than running "perl -de0", and doing two line assignments.
Who cares, our boxes have plenty of RAM.
Madness, thy name is system administration
Mike_Ca Li
Regular Advisor

Re: grep function in perl

variable=/ABC/SVR; ps -ef | grep $variable
returns
/ABC/SVR1
/ABC/SVR
/ABC/SVR3
/ABC/SVR4
etc

variable=/ABC/SVR; ps -ef | grep -w $variable
returns nothing, but I wanted /ABC/SVR

I also want to get /ABC/SVR from:

variable=/ABC/SVR; perl -e 'print grep(/\b$variable\b/, `ps -ef`)'
does not return anything and I wanted /ABC/SVR

Please advise.


Mike_Ca Li
Regular Advisor

Re: grep function in perl

However,
perl -e 'print grep(/\bSVR\b/, `ps -ef`)'
does print only /ABC/SVR

but

perl -e 'print grep(/\b\/ABC\/SVR\b/, `ps -ef`)'
returns nothing. I'm confused and what am I doing wrong.
H.Merijn Brand (procura
Honored Contributor

Re: grep function in perl

Hah! HAH!

You were not specific enough in the first quest. You were refering to an EXTERNAL or ENVIRONMENT (shell) variable, not to a perl variable

variable=/ABC/SVR; perl -e 'print grep(/\b$ENV{variable}\b/, `ps -ef`)'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Mike_Ca Li
Regular Advisor

Re: grep function in perl

HI:
variable=/ABC/SVR; perl -e 'print grep(/\b$ENV{variable}\b/,`ps -ef`)'
is returning all the systems processes :(
Any ideas? Thanks
H.Merijn Brand (procura
Honored Contributor

Re: grep function in perl

yes I have, but I do not know if that is applicable in your shell

$ variable=blah;echo $variable; perl -le'print$ENV{variable}'
blah

$ export variable=blah;echo $variable; perl -le'print$ENV{variable}'
blah
blah
$ env variable=blah perl -le'print$ENV{variable}'
blah
$

you are setting a 'shell' variable, not an environment variable. Variables set the way you do, are not available to other processes than the shell itself
You will need either export or env

Now you grep using a $ENV that's empty, so you grep on /\b\b/, which is probably not what you tought to do.

I'm using a tcsh, which would need setenv instead of export. 'env' works in all shells

Enjoy, Have FUN! H.Merijn [ think about your points please ]
Enjoy, Have FUN! H.Merijn
Mike_Ca Li
Regular Advisor

Re: grep function in perl

export variable=/NSG/SVR; perl -le 'print grep(/$ENV{variable}/,`ps -ef`)'
returns:
/NSG/SVR3
/NSG/SVR
/NSG/SVR2
/NSG/SVR1
etc

but:
export variable=/NSG/SVR; perl -le 'print grep(/\b$ENV{variable}\b/,`ps -ef`)'
returns nothing

More points issued if able to find a solution. Thanks
Mike_Ca Li
Regular Advisor

Re: grep function in perl

variable=/ABC/SVR; perl -le 'print grep(/$ENV{variable}\b/,`ps -ef`)'
returns /ABC/SVR

Thanks all for replies
H.Merijn Brand (procura
Honored Contributor

Re: grep function in perl

What if you drop the first \b?
I'm still baffled though. This should work!

Totally besides the point: the paren's are not needed with grep.

# export variable=/NSG/SVR; perl -le 'print grep(/\b$ENV{variable}\b/,`ps -ef`)'

Just to easy my curiousity: what does env do?

# env variable=/NSG/SVR perl -le 'print grep /\b$ENV{variable}\b/, `ps -ef`'

And what perl version do you use

# perl -v

Enjoy, Have FUN! H.Merijn [ please point *all* answers. 0 is better than unassigned ]
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: grep function in perl

No points for this, but here's how you couls have done it with Proc::ProcessTable:

# perl -MProc::ProcessTable -le'for(@{Proc::ProcessTable->new()->table}){($p=$_->cmndline)=~/$ENV{variable}\b/&&print$p}'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn