Click here to get back home

Question on regex

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Question on regex cyrusgreats 03-07-2008
---> Re: Question on regex Gunnar Hjalmars...03-07-2008
Posted by Martijn Lievaart on March 7, 2008, 6:01 pm
Please log in for more thread options
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

Posted by cyrusgreats on March 7, 2008, 6:31 pm
Please log in for more thread options
> 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..

Posted by Gunnar Hjalmarsson on March 7, 2008, 7:04 pm
Please log in for more thread options
cyrusgreats@gmail.com wrote:
> 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 for one am not able to reproduce that result. For me, the script
filters lines with '0.0' as expected.

Of course, adding the * quantifier to the whitespace is advisable, and
you also want to escape the dot

next if $line =~ /^\s*0\.0/;
-------------------------^-^

or else it matches any character. Furthermore, since you don't chomp the
output, printing of the extra "\n" is redundant.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by szr on March 7, 2008, 8:26 pm
Please log in for more thread options
cyrusgreats@gmail.com wrote:
>> 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..

What you're trying to do can more easily be accomplished with this one
liner:

ps -eo pcpu,pid,user,args | egrep -v '^ 0.0'


Enjoy :-)

--
szr



Posted by szr on March 7, 2008, 8:22 pm
Please log in for more thread options
Martijn Lievaart wrote:
> 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>) {

Maybe make this:

while (chomp(my $line = <DATA>)) {

That will eat the trailing new line.


> next if $line =~ /^\s0.0/; # skip 0.0

Make that:

next if $line =~ /^\s0\.0/;

(note the \. which matches a literal period/decimal point :-) )

--
szr



Similar ThreadsPosted
Little question on regex. October 28, 2004, 1:28 pm
Regex question April 18, 2005, 10:50 am
Yet another regex question. April 18, 2005, 11:23 am
a regex question .. April 30, 2005, 6:20 pm
regex question January 6, 2006, 1:36 pm
Regex Question April 3, 2006, 11:02 pm
Regex question April 8, 2006, 6:18 pm
regex question April 13, 2006, 6:46 pm
Regex question October 31, 2006, 10:31 am
regex question December 1, 2006, 12:21 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap