Solved

Get value from row by column name

Posted on 2013-12-20
1
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Utilizing an array to gracefully append to a list of EmailAddresses
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…

710 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