Question on regex

C

cyrusgreats

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..
 
G

Gunnar Hjalmarsson

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/clpmisc_guidelines.html
 
C

cyrusgreats

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/clpmisc_guidelines.html
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";
}
 
W

Willem

(e-mail address 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/clpmisc_guidelines.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
 
J

J. Gleixner

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/;
 
C

cyrusgreats

(e-mail address 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/clpmisc_guidelines.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
 
C

cyrusgreats

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


----------------------------------------------------------

----------------------------------------------------------
color]

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
 
P

patrick

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
 
M

Martijn Lievaart

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
 
C

cyrusgreats

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..
 
G

Gunnar Hjalmarsson

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.
 
M

Martijn Lievaart

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 get completely different output (also on Linux):
%CPU PID USER COMMAND

6.3 3104 root X :0 -auth /home/martijn/.serverauth.3087

0.1 3256 martijn /usr/libexec/clock-applet --oaf-activate-
iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=19

0.7 3257 martijn /usr/libexec/multiload-applet-2 --oaf-activate-
iid=OAFIID:GNOME_MultiLoadApplet_Factory --oaf-ior-fd=25

0.1 3294 martijn gnome-terminal

0.1 5913 martijn ssh dexter

3.1 13381 martijn ./client/civclient

0.4 13623 martijn /usr/lib/ICAClient/wfica -display :0.0 -icaroot /usr/
lib/ICAClient -nosplash -desc Sanquin Desktop -startSCD 1204932778777

1.0 13719 martijn ps -eo pcpu,pid,user,args

4.8 18872 martijn /usr/lib64/firefox-2.0.0.10/firefox-bin http://
www.thinkgeek.com/geektoys/warfare/a1f7/?cpg=68T

1.7 20170 martijn gtk-gnutella

0.1 26130 martijn /usr/lib64/thunderbird-2.0.0.9/thunderbird-bin

0.1 27625 martijn ./server/civserver -r options.serv

Are you sure this is the program you use? My output is completely what I
expect it to be. All lines with 0.0 are surpressed and there is still an
extra \n due to the missing chomp.

Something is not adding up here, and my guess is that both your program
and your output are not what you are posting here. Or there is something
else, but I'm completely at a loss what that could be.

HTH,
M4
 
C

cyrusgreats

Something is not adding up here, and my guess is that both your program
and your output are not what you are posting here. Or there is something
else, but I'm completely at a loss what that could be.

HTH,
M4

found the problem, I print out @output and found that it is not an
array, I add the following and seems works fine now..

my $output = '$cmd';
my @stack = split /\n/, $output;


Thanks all of you guys and have great weekend and stay cool ;-)
 
S

szr

Martijn said:
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 :) )
 
S

szr

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 :)
 
S

szr

patrick said:
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

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

Which allows for a variable amount of leading spaces (or none at all.)
 
B

Ben Morrow

Quoth (e-mail address removed):
found the problem, I print out @output and found that it is not an
array, I add the following and seems works fine now..

my $output = '$cmd';

You mean `` here. Please stop getting this wrong, or stop retyping code
and copy-paste it instead.
my @stack = split /\n/, $output;

`` in list context splits the results on $/. If this doesn't appear to
be happening, then you must have set $/ to something funny earlier in
the script. This is why you are asked to post a complete script you have
actually run, rather than just fragments.

Ben
 
D

Dave Weaver

Maybe make this:

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

That will eat the trailing new line.

And yet it's not the right way to do it.

If you have warnings enabled (as you should), when you read the
last line of the file, line is undefined and you will get
a warning:
Use of uninitialized value in chomp at ...

Better to write:
while (my $line = <DATA>) {
chomp $line;
...
}
 

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,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top