Search code examples
linuxshellcommand-linesh

Shell script program - Filter rows from log file which having disk-space utilization size more than 80%


I have one following log file (input-log-file.log) and i want to do filter on log file using shell script (.sh) which give me rows which having disk space utilization more than 80% (>= 80) along with server name in output file.

input-log-file.log :

====================== Server-1: [email protected] ======================
/data/file/test 50G 5.3G 45G 11% /opt/apps/user

====================== Server-2: [email protected] ======================
/data/xyz/test 100G 30G 45G 86% /opt/apps/user

====================== Server-3: [email protected] ======================
/dev/env/volume 5.0G 418M 4.6G 55% /opt/apps/user
/dev/env/volume 100G 59G 42G 59% /opt/apps/user
/dev/env/volume 200G 51G 150G 92% /opt/apps/user/abc1


====================== Server-4: [email protected] ======================
/dev/env/flow_vol 100G 60G 41G 82% /opt/apps/user
/dev/env/flow_vol/data 100G 39G 62G 39% /opt/apps/user
/dev/env/flow_vol/data 100G 20G 81G 20% /opt/apps/user/xyz
/dev/env/flow_vol/test 200G 66G 135G 96% /opt/apps/user/

My excepted output in new file as below (i.e. output-log-file.log):

====================== Server-2: [email protected] ======================
/data/xyz/test 100G 30G 45G 86% /opt/apps/user

====================== Server-3: [email protected] ======================
/dev/env/volume 200G 51G 150G 92% /opt/apps/user/abc1

====================== Server-4: [email protected] ======================
/dev/env/flow_vol 100G 60G 41G 82% /opt/apps/user
/dev/env/flow_vol/test 200G 66G 135G 96% /opt/apps/user/

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

I written below shell script which give me rows which having disk space utilization more than 80% (>= 80) but I am NOT getting the server name.

disk-space-filter.sh file :

awk '{original_block=$0; FS=" "; gsub(/\(|\)|%/,"");  if($5>=80) print original_block }' input-log-file.log > output-log-file.log

If i run the above shell script my program (./disk-space-filter.sh) then I am able to get the below output in new file but not getting server name :

/data/xyz/test 100G 30G 45G 86% /opt/apps/user
/dev/env/volume 200G 51G 150G 92% /opt/apps/user/abc1
/dev/env/flow_vol 100G 60G 41G 82% /opt/apps/user
/dev/env/flow_vol/test 200G 66G 135G 96% /opt/apps/user/

Any help would be really appreciated?


Solution

  • $ awk -F'[ %]' -v n=80 '
        NF==4{
            head=$0
        }
        $5>=n && head{
            print head; head=""
        }
        $5>=n
    ' file
    
    ====================== Server-2: [email protected] ======================
    /data/xyz/test 100G 30G 45G 86% /opt/apps/user
    ====================== Server-3: [email protected] ======================
    /dev/env/volume 200G 51G 150G 92% /opt/apps/user/abc1
    ====================== Server-4: [email protected] ======================
    /dev/env/flow_vol 100G 60G 41G 82% /opt/apps/user
    /dev/env/flow_vol/test 200G 66G 135G 96% /opt/apps/user/