Find the answer to your Linux question:
Results 1 to 4 of 4
GNU Awk 3.1.1 on a RHEL 3 U 5 server: Simple little script to highlight disc partitions over 90% and 75% Weirdness is that file partitions at 9% get highlighted ...
  1. #1
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539

    awk Oddity?

    GNU Awk 3.1.1 on a RHEL 3 U 5 server:

    Simple little script to highlight disc partitions over 90% and 75%
    Weirdness is that file partitions at 9% get highlighted yellow (green in example for clarity). If I "PCENT=9", then it works fine?

    I used %s in the "Why is" statement to see if there was any whitespace lurking, but nothing other than 9 is printed...

    The code (any suggestions for improvement welcome)

    Code:
    #!/bin/bash
    df -PT | awk '
    {
      if ($1!="Filesystem") {
        PCENT=substr($6, 1, length($6)-1)
        COL=0
        if (PCENT >= 90){
          printf "\033[1;31;40m";
          COL=1
        } else
        if (PCENT >= 75){
    printf "Why is [%s] >= 75\n",PCENT;
          printf "\033[1;33;40m";
          COL=1
        }
        printf "%-28s %-6s %11d\t%11d\t%11d\t%7d%% %s\n", $1,$2,$3,$4,$5,PCENT,$7;
      }
      else printf "%-28s %-6s %s\t%11s\t%11s\t%s %s %s\n", $1,$2,$3,$4,$5,$6,$7,$8;
      if (COL=1) printf "\033[0;m"}
    '
    Code:
    Filesystem                   Type   1024-blocks        Used       Available     Capacity Mounted on
    /dev/vg00/lv00               ext3       1032088      157508          822152          17% /
    Why is [9] >= 75
    /dev/cciss/c0d0p1            ext3        126419       10341          109551           9% /boot
    /dev/vg00/lv01               ext3        507748       12099          469435           3% /home
    /dev/vg00/lv06               ext3      20642428     1332012        18261840           7% /opt
    none                         tmpfs      2015504           0         2015504           0% /dev/shm
    /dev/vg00/lv02               ext3       1588452       32820         1474944           3% /tmp
    Why is [9] >= 75
    /dev/vg00/lv03               ext3       3096336      246728         2692324           9% /var
    /dev/vg00/lv04               ext3       2064208      835108         1124244          43% /usr
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    No oddities here. PCENT is a string. You need to use int(PCENT) if you are going to do numerical comparisons.

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    Thanks burschik,

    I wrapped it around the substr statement.
    Getting to used to those untyped languages

    Works a treat now.
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Using FS in the BEGIN block can save you some codes:
    Code:
    
    #!/bin/bash
    df -PT | awk '
    BEGIN { 
            FS="[% ]+"
    }
    {
            if ($5>=90&&NR>1)
            {
                  printf "\033[1;31;40m%s\033[0;m\n",$0;
            }
            else
            {
                    if($5 >=75&&NR>1)
                    {
                    printf "\033[1;33;40m%s\033[0;m\n",$0;
                    }
                    else printf "%s\n",$0;
            }
    }
    '

Posting Permissions

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