Solved

Get value from row by column name

Posted on 2013-12-20
1
331 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a python script 5 87
delete a folder ever 5 minutes 6 60
Best way to split and output to csv in bash 2 60
Determine Who is Runnig my Bash Shell Script 4 60
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now