Solved

Get value from row by column name

Posted on 2013-12-20
1
336 Views
Last Modified: 2014-01-29
Hi,
I have output of mpstat. in different Linux OS it give different column numbers and  location of "%idle" column

I need to get value of row  "Average" and column "%idle" (55.93) using awk, sed, etc. Shell - ksh

Linux 2.6.18-308.13.1.el5 (Server)    12/20/2013
08:30:49 AM  CPU   %user   %nice    %sys %iowait    %irq   %soft  %steal   %idle    intr/s
08:30:51 AM  all   44.72    0.00   15.33    4.52    0.00    0.00    0.00   35.43   1186.43
08:30:53 AM  all    8.04    0.00    6.28   20.35    0.25    0.75    0.00   64.32   1520.10
08:31:03 AM  all    8.27    0.00    5.01    8.52    0.25    1.50    0.00   76.44   2635.50
08:31:05 AM  all    9.25    0.00    3.25    8.75    0.25    1.50    0.00   77.00   2787.00
Average:     all   11.75    0.00    8.49   22.62    0.26    0.96    0.00   55.93   1824.01


Thanks in advance
0
Comment
Question by:IKeystone
1 Comment
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 39732725
Here is a beginning:
#!/bin/bash
#!/bin/ksh
# 
IFS=' '
PLATFORM=`/bin/uname`
case $PLATFORM in
  Linux)
    #12:24:33 PM  CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
    #  1       2    3   4    5     6     7      8     9    10     11     12
    #Average: all %usr %nice %sys %iowait %irq %soft %steal %guest %idle
    VARFLD='4,6,12'   #<<--- Set the field numbers you want to extract
    MPSTAT='mpstat '  # Next commands will select the rows you want:
    STATIN=$($MPSTAT |tail -n +2 | awk -F' ' 'BEGIN{ORS="\n"}/CPU|all/{print}')
    ;;
  AIX)
    #cpu min maj mpc int cs ics rq mig lpa sysc us sy wa id pc %ec lcs
    #  1   2   3   4   5  6   7  8   9  10   11 12 13 14 15 16  17  18
    VARFLD='12,13,14,15'
    MPSTAT='mpstat 1 1'  
    STATIN=$($MPSTAT | awk -F' ' 'BEGIN{ORS="\n"}/^cpu|ALL/{print}')
    ;;
  ## You can complete these others:
  HP-UX|HI-UX)
    MPSTAT='sar -A -S 1 1'  
    ;;
  SunOS)
    MPSTAT='mpstat 1 1'    
    ;;
  OSF1)
    MPSTAT='sar -S'  
    ;;
esac

echo $STATIN |\
awk -F' ' -v fields="$VARFLD" '
NR == 1 {for(i=1;i<=NF;++i){h[i]=$i}; next}
{n=split(fields,arr,",");
 print "fields="n":"fields;
 for(i in arr){printf "%s;",h[arr[i]]}; printf "\n"
 for(i in arr){k=arr[i]; printf "%s;",$k} printf "\n"}'

Open in new window

Enjoy!
;)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question