Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get value from row by column name

Posted on 2013-12-20
1
Medium Priority
?
343 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 1500 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

916 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