Question on regex

Discussion in 'Perl Misc' started by cyrusgreats@gmail.com, Mar 7, 2008.

  1. Guest

    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..
     
    , Mar 7, 2008
    #1
    1. Advertising

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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 7, 2008
    #2
    1. Advertising

  3. Guest

    > 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";
    }
     
    , Mar 7, 2008
    #3
  4. Willem Guest

    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
     
    Willem, Mar 7, 2008
    #4
  5. J. Gleixner Guest

    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";
    > }
    >


    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/;
     
    J. Gleixner, Mar 7, 2008
    #5
  6. Guest

    On Mar 7, 2:07 pm, Willem <> wrote:
    > 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
     
    , Mar 7, 2008
    #6
  7. Guest


    > 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
     
    , Mar 7, 2008
    #7
  8. patrick Guest

    >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
     
    patrick, Mar 7, 2008
    #8
  9. 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
     
    Martijn Lievaart, Mar 7, 2008
    #9
  10. Guest

    > 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..
     
    , Mar 7, 2008
    #10
  11. 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
     
    Gunnar Hjalmarsson, Mar 8, 2008
    #11
  12. On Fri, 07 Mar 2008 15:31:09 -0800, cyrusgreats 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 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
     
    Martijn Lievaart, Mar 8, 2008
    #12
  13. Guest


    >
    > 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 ;-)
     
    , Mar 8, 2008
    #13
  14. szr Guest

    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
     
    szr, Mar 8, 2008
    #14
  15. szr Guest

    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
     
    szr, Mar 8, 2008
    #15
  16. szr Guest

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


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

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

    --
    szr
     
    szr, Mar 8, 2008
    #16
  17. Ben Morrow Guest

    Quoth :
    >
    > >
    > > 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';


    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
     
    Ben Morrow, Mar 8, 2008
    #17
  18. Dave Weaver Guest

    On Fri, 7 Mar 2008 17:22:15 -0800, szr <> wrote:
    > > On Fri, 07 Mar 2008 14:19:49 -0800, cyrusgreats wrote:
    > >
    > > while (my $line = <DATA>) {

    >
    > 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;
    ...
    }
     
    Dave Weaver, Mar 11, 2008
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    724
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,651
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    614
  4. Xah Lee
    Replies:
    1
    Views:
    956
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    797
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page