Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Regex pattern matching

  1. #11
    Join Date
    Dec 2007
    Beans
    11,113

    Re: Regex pattern matching

    Quote Originally Posted by steeldriver View Post
    I don't know - top has *lots* of configuration options (plus optional global / personal rc files)... or it could just be the top version? mine is

    Code:
    $ top --version
        top: procps version 3.2.8
    Mine is:
    Code:
    [06:32 PM] ~ $ top -v
    	procps-ng version 3.3.3
    usage:	top -hv | -bcHiSs -d delay -n limit -u|U user | -p pid[,pid] -w [cols]
    [06:32 PM] ~ $
    and it didn't like top --version!
    Users can easily damage their systems by using sudo su if they aren't experienced enough.

  2. #12
    Join Date
    Dec 2007
    Beans
    11,113

    Re: Regex pattern matching

    As for the use of + before (or as part of) the look ahead assertion, maybe -P is also different?
    Code:
           -P, --perl-regexp
                  Interpret  PATTERN  as  a  Perl  regular  expression  (PCRE, see
                  below).  This is highly experimental and grep  -P  may  warn  of
                  unimplemented features.
    Users can easily damage their systems by using sudo su if they aren't experienced enough.

  3. #13
    Join Date
    Sep 2012
    Beans
    94
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Regex pattern matching

    Hi all,
    i try to put in on a bash script.
    But i encountered an error.
    Here my script:
    Code:
    #!/bin/bash
    cpu=$(top -bn1 | grep -oP '\d.\d(?=%us)')
    #echo "Test $cpu "
    if[$cpu > 0.0];then
    echo "High CPU usage : $cpu "
    else
    echo "CPU is normal : $cpu "
    fi
    The error:
    Code:
    ./cpu.sh: line 4: syntax error near unexpected token `then'
    ./cpu.sh: line 4: `if[$cpu > 0.0];then'
    I think the if else is correct. Maybe because of the variable in format 0.0 ?

  4. #14
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Regex pattern matching

    The syntax of the if statement is:
    Code:
    if COMMANDS; then OTHER COMMANDS; fi
    You need a space after `if'.

    Also BASH arithmetic uses integers only. For floating point numbers you have to use an external program like bc or dc:
    Code:
    if (( $(bc <<< "$cpu > 0.0") )); then ...
    See:
    http://mywiki.wooledge.org/BashGuide..._and_.5B.5B.29
    and BashFAQ 022 (link in my signature).

  5. #15
    Join Date
    Sep 2012
    Beans
    94
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Regex pattern matching

    Hi,

    Thanks for your reply.
    This script is work for me, but need to install 'bc' first.
    Code:
    #!/bin/bash
    cpu=$(top -bn1 | grep -oP '\d\d.\d(?=%us)')
    #echo "Test $cpu "
    if (( $(bc <<< "$cpu > 0.0") )); then
        echo "High CPU usage : $cpu "
    else
        echo "CPU is normal : $cpu "
    fi
    And use this code to generate some CPU load to test.

    Code:
    #!/bin/bash
    
    while : ; do
    true
    done
    Thanks again.

    p/s: i edit the regex pattern to match 00.0 format.
    Last edited by termvrl; November 19th, 2013 at 04:58 PM.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •