Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question on regex

Reply
Thread Tools

Question on regex

 
 
cyrusgreats@gmail.com
Guest
Posts: n/a
 
      03-07-2008
I'm using the following regex using perl to get anything that is not
0.0 but it doesn't work the way I want it, I need to print those that
are not 0.0

The string is:

0.0 19968 admin /bin/bash -l
1.0 20037 admin /bin/bash -l
0.2 20085 admin /bin/bash -l
0.0 20363 admin /bin/bash -l

next if $line =~ /^\s0.0/; # skip 0.0
print $line, "\n";

the out put is:
next if $line =~ /^\s0.0/; # skip 0.0

But I all I want is:

1.0 20037 admin /bin/bash -l
0.2 20085 admin /bin/bash -l

Anyone out there know how to fix this one?
Thanks in advance..
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-07-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm using the following regex using perl to get anything that is not
> 0.0 but it doesn't work the way I want it, I need to print those that
> are not 0.0
>
> The string is:
>
> 0.0 19968 admin /bin/bash -l
> 1.0 20037 admin /bin/bash -l
> 0.2 20085 admin /bin/bash -l
> 0.0 20363 admin /bin/bash -l
>
> next if $line =~ /^\s0.0/; # skip 0.0
> print $line, "\n";
>
> the out put is:
> next if $line =~ /^\s0.0/; # skip 0.0
>
> But I all I want is:
>
> 1.0 20037 admin /bin/bash -l
> 0.2 20085 admin /bin/bash -l


Please post a short but complete program to show us what you are doing,
as is suggested in the posting guidelines for this group:
http://www.rehabitation.com/clpmisc/...uidelines.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
cyrusgreats@gmail.com
Guest
Posts: n/a
 
      03-07-2008
> Please post a short but complete program to show us what you are doing,
> as is suggested in the posting guidelines for this group:http://www.rehabitation.com/clpmisc/...uidelines.html
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl

Well that depends of the question, the question is very straight
forward, anyway here is portion of the code..again thanks..

my $cmd = "ps -eo pcpu,pid,user,args";
my @output = '$cmd';
foreach my $line (@output) {
next if $line =~ /\s0.0/; # skip 0.0
print $line, "\n";
}

 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      03-07-2008
(E-Mail Removed) wrote:
)> Please post a short but complete program to show us what you are doing,
)> as is suggested in the posting guidelines for this group:http://www.rehabitation.com/clpmisc/...uidelines.html
)>
)> --
)> Gunnar Hjalmarsson
)> Email:http://www.gunnar.cc/cgi-bin/contact.pl
) Well that depends of the question, the question is very straight
) forward, anyway here is portion of the code..again thanks..
)
) my $cmd = "ps -eo pcpu,pid,user,args";
) my @output = '$cmd';
) foreach my $line (@output) {
) next if $line =~ /\s0.0/; # skip 0.0
) print $line, "\n";
) }

First: The dot matches any character in a regexp.
Second: I don't see the caret at the start of the regexp to anchor it.
This will probably result in spurious matches.
Third: The lines will already have newlines on them so the extra one
in the print statement will produce blank lines.

Oh, and fourth: in the original question, I *did* see the caret at the
start, which made the question impossible to answer as such, let alone
straight forward. This is one reason why the best way is to copy-paste
the relevant bits of your code. All characters count.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      03-07-2008
(E-Mail Removed) wrote:
>> Please post a short but complete program to show us what you are doing,
>> as is suggested in the posting guidelines for this group:http://www.rehabitation.com/clpmisc/...uidelines.html
>>
>> --
>> Gunnar Hjalmarsson
>> Email:http://www.gunnar.cc/cgi-bin/contact.pl

> Well that depends of the question, the question is very straight
> forward, anyway here is portion of the code..again thanks..
>
> my $cmd = "ps -eo pcpu,pid,user,args";
> my @output = '$cmd';
> foreach my $line (@output) {
> next if $line =~ /\s0.0/; # skip 0.0
> print $line, "\n";
> }
>


Hu.. it seems to work just fine.

That prints:

$cmd

which doesn't match your pattern.

Maybe you want:

my @output = `$cmd`;

and

next if $line =~ /^\s0\.0/;
 
Reply With Quote
 
cyrusgreats@gmail.com
Guest
Posts: n/a
 
      03-07-2008
On Mar 7, 2:07 pm, Willem <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>
> )> Please post a short but complete program to show us what you are doing,
> )> as is suggested in the posting guidelines for this group:http://www.rehabitation.com/clpmisc/...uidelines.html
> )>
> )> --
> )> Gunnar Hjalmarsson
> )> Email:http://www.gunnar.cc/cgi-bin/contact.pl
> ) Well that depends of the question, the question is very straight
> ) forward, anyway here is portion of the code..again thanks..
> )
> ) my $cmd = "ps -eo pcpu,pid,user,args";
> ) my @output = '$cmd';
> ) foreach my $line (@output) {
> ) next if $line =~ /\s0.0/; # skip 0.0
> ) print $line, "\n";
> ) }
>
> First: The dot matches any character in a regexp.
> Second: I don't see the caret at the start of the regexp to anchor it.
> This will probably result in spurious matches.
> Third: The lines will already have newlines on them so the extra one
> in the print statement will produce blank lines.
>
> Oh, and fourth: in the original question, I *did* see the caret at the
> start, which made the question impossible to answer as such, let alone
> straight forward. This is one reason why the best way is to copy-paste
> the relevant bits of your code. All characters count.
>
> SaSW, Willem
> --
> Disclaimer: I am in no way responsible for any of the statements
> made in the above text. For all I know I might be
> drugged or something..
> No I'm not paranoid. You all think I'm paranoid, don't you !
> #EOT


ok, how about this, what if and only if I want to match anything but
not 0.0, since the above doesn't work..if the string as follows, I'm
interested in 0.2 not 0.0.
space 0.0 somewords
space 0.2 somewords
 
Reply With Quote
 
cyrusgreats@gmail.com
Guest
Posts: n/a
 
      03-07-2008

> That code works on my system. What output do you get on your system?
>
> To avoid false positives, you might want to use the following regex
> instead:
>
> next if $line =~ m{ \A \s* 0\.0 }x;
>
> --
> Jim Gibson
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com


I get all the lines as follows:
0.0 19968 admin /bin/bash -l
1.0 20037 admin /bin/bash -l
0.2 20085 admin /bin/bash -l
0.0 20363 admin /bin/bash -l
 
Reply With Quote
 
patrick
Guest
Posts: n/a
 
      03-07-2008
>my $cmd = "ps -eo pcpu,pid,user,args";

On AIX I get two leading spaces before the 0.0
On RHL I get only one space.

You can always change to ps -eo pcpu,pid,user,args | grep -v '^ 0.0'

====>Patrick

 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      03-07-2008
On Fri, 07 Mar 2008 14:19:49 -0800, cyrusgreats wrote:

> ok, how about this, what if and only if I want to match anything but not
> 0.0, since the above doesn't work..if the string as follows, I'm
> interested in 0.2 not 0.0.
> space 0.0 somewords
> space 0.2 somewords


You still don't get it. Your original code did match these lines. You say
it did not work. That does not add up. We're not clairvoyant here, so we
cannot determine what the problem really is. Post a small _but_complete_
program that shows your problem.

F.i.

#!/usr/bin/perl

use strict;
use warnings;

while (my $line = <DATA>) {
next if $line =~ /^\s0.0/; # skip 0.0
print $line, "\n";
}

__DATA__
0.0 19968 admin /bin/bash -l
1.0 20037 admin /bin/bash -l
0.2 20085 admin /bin/bash -l
0.0 20363 admin /bin/bash -l

This outputs:

1.0 20037 admin /bin/bash -l

0.2 20085 admin /bin/bash -l

Which is exactly what I would expect. (Hint, you're missing a chomp
somewhere).

HTH,
M4
 
Reply With Quote
 
cyrusgreats@gmail.com
Guest
Posts: n/a
 
      03-07-2008
> You still don't get it. Your original code did match these lines. You say
> it did not work. That does not add up. We're not clairvoyant here, so we
> cannot determine what the problem really is. Post a small _but_complete_
> program that shows your problem.
>
> F.i.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> while (my $line = <DATA>) {
> next if $line =~ /^\s0.0/; # skip 0.0
> print $line, "\n";
>
> }
>
> __DATA__
> 0.0 19968 admin /bin/bash -l
> 1.0 20037 admin /bin/bash -l
> 0.2 20085 admin /bin/bash -l
> 0.0 20363 admin /bin/bash -l
>
> This outputs:
>
> 1.0 20037 admin /bin/bash -l
>
> 0.2 20085 admin /bin/bash -l
>
> Which is exactly what I would expect. (Hint, you're missing a chomp
> somewhere).
>
> HTH,
> M4



Well, let's start over again:


from Linux if I send following command I get the following output:
[root@MyWorld]#ps -eo pcpu,pid,user,args
%CPU PID USER COMMAND
0.0 19968 admin /bin/bash -l
1.0 20037 admin /bin/bash -l
0.2 20085 admin /bin/bash -l
0.0 20363 admin /bin/bash -l

My script below supposed to match lines that are not 0.0 such as as
0.2 & 1.0.

#!/usr/bin/perl

use strict;
use warnings;
my $cmd = "ps -eo pcpu,pid,user,args";
my @output = `$cmd`;

foreach my $line (@output) {
next if $line =~ /^\s0.0/; # skip 0.0
print $line, "\n";
}


The out put I'm getting from the above code is:
%CPU PID USER COMMAND
0.0 19968 admin /bin/bash -l
1.0 20037 admin /bin/bash -l
0.2 20085 admin /bin/bash -l
0.0 20363 admin /bin/bash -l

I used the while loop as Martijn Lievaart suggested above but I don't
get any output at all!
Thanks in advance guys and be patient with me it's Friday..
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments