- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- grep function in perl
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 03:20 AM
06-27-2005 03:20 AM
@ps = grep(/$variable*/, @ps);
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 03:30 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 03:30 AM
06-27-2005 03:30 AM
Re: grep function in perl
Enjoy, Have FUN! H.Merijn [ a more detailed question could have caused a more verbose answer ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 03:55 AM
06-27-2005 03:55 AM
Re: grep function in perl
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 03:59 AM
06-27-2005 03:59 AM
Re: grep function in perl
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 03:59 AM
06-27-2005 03:59 AM
Re: grep function in perl
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 04:23 AM
06-27-2005 04:23 AM
Re: grep function in perl
where to find detailed documentation of \b$variable\b.
perldoc -f grep is very short.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 04:56 AM
06-27-2005 04:56 AM
Re: grep function in perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 05:28 AM
06-27-2005 05:28 AM
Re: grep function in perl
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 05:34 AM
06-27-2005 05:34 AM
Re: grep function in perl
I just verified in the debugger,
procura's right.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 05:38 AM
06-27-2005 05:38 AM
Re: grep function in perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 05:49 AM
06-27-2005 05:49 AM
Re: grep function in perl
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 05:58 AM
06-27-2005 05:58 AM
Re: grep function in perl
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 06:23 AM
06-27-2005 06:23 AM
Re: grep function in perl
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 06:31 AM
06-27-2005 06:31 AM
Re: grep function in perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 06:40 AM
06-27-2005 06:40 AM
Re: grep function in perl
variable=/ABC/SVR; perl -e 'print grep(/\b$ENV{variable}\b/,`ps -ef`)'
is returning all the systems processes :(
Any ideas? Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 06:48 AM
06-27-2005 06:48 AM
Re: grep function in perl
$ 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 ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 07:36 AM
06-27-2005 07:36 AM
Re: grep function in perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 07:44 AM
06-27-2005 07:44 AM
Re: grep function in perl
returns /ABC/SVR
Thanks all for replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 07:48 AM
06-27-2005 07:48 AM
Re: grep function in perl
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 ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2005 07:53 AM
06-27-2005 07:53 AM
Re: grep function in perl
# perl -MProc::ProcessTable -le'for(@{Proc::ProcessTable->new()->table}){($p=$_->cmndline)=~/$ENV{variable}\b/&&print$p}'
Enjoy, Have FUN! H.Merijn