Solved

Get value from row by column name

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

15 Experts available now in Live!

Get 1:1 Help Now