Storing two dimensional array for postprocessing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing two dimensional array for postprocessing
# 1  
Old 11-07-2016
Storing two dimensional array for postprocessing

Hi Community,

Would love to get some quick help on below requirement.

I am trying to process mpstat output from multiple blades of my server
I would like to assign this the output to an array and then use it for post processing. How can I use a two dimensional array and assign these value

Desired output
Code:
        cpuusage(<CPU NO>,<Type>)

e.g.: cpuusage(2,irq) will return 0.04

Code:
mpstat -P ALL
Linux 3.0.101-0.15.1.6550.0.PTF-default (oam)  11/07/16        _x86_64_

14:58:33     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
14:58:33     all    0.07    0.00    0.09    0.00    0.00    0.02    0.00    0.00   99.82
14:58:33       0    0.14    0.00    0.09    0.00    0.00    0.02    0.00    0.00   99.75
14:58:33       1    0.07    0.00    0.07    0.00    0.00    0.01    0.00    0.00   99.85
14:58:33       2    0.07    0.00    0.12    0.00    0.00    0.04    0.00    0.00   99.77
14:58:33       3    0.22    0.00    0.20    0.00    0.00    0.05    0.00    0.00   99.53
14:58:33       4    0.12    0.00    0.15    0.00    0.00    0.01    0.00    0.00   99.72
14:58:33       5    0.11    0.00    0.18    0.00    0.00    0.02    0.00    0.00   99.69
14:58:33       6    0.08    0.00    0.04    0.00    0.00    0.00    0.00    0.00   99.88
14:58:33       7    0.07    0.00    0.05    0.00    0.00    0.00    0.00    0.00   99.88
14:58:33       8    0.07    0.00    0.04    0.00    0.00    0.00    0.00    0.00   99.89
14:58:33       9    0.12    0.00    0.05    0.00    0.00    0.00    0.00    0.00   99.83
14:58:33      10    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
14:58:33      11    0.03    0.00    0.09    0.00    0.00    0.04    0.00    0.00   99.85
14:58:33      12    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.94
14:58:33      13    0.07    0.00    0.03    0.00    0.00    0.00    0.00    0.00   99.90
14:58:33      14    0.03    0.00    0.40    0.00    0.00    0.18    0.00    0.00   99.39
14:58:33      15    0.05    0.00    0.10    0.00    0.00    0.00    0.00    0.00   99.84
14:58:33      16    0.07    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.91
14:58:33      17    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
14:58:33      18    0.02    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
14:58:33      19    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95


Last edited by Don Cragun; 11-07-2016 at 01:48 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 11-07-2016
WHERE do you want to create that array (shell, text utility e.g. awk, a c programme)? How to pass it to post processing?

Why does cpuusage(2,irq) return 0.04 and not 0.00?
# 3  
Old 11-07-2016
I am trying to use it in a shell script

Post processing is like a Performance Management File.

I sample CPU usage every 5mins, then average it for a 15min period. Store the stats into a file

Yes, sorry its my mistake. It should look like
Code:
cpuusage(2,soft) = 0.04


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-07-2016 at 07:15 AM.. Reason: Added CODE tags.
# 4  
Old 11-07-2016
You failed to mention the shell you use. In case it's bash, try "associative arrays":
Code:
declare -A cpuusage
{ read
  read
  read -a HD
  while read -a TMP
    do for i in ${!HD[@]}
         do cpuusage[${TMP[1]}","${HD[$i]}]=${TMP[$i]}
         done
    done
} < file
echo ${cpuusage[2,%soft]}
0.04

# 5  
Old 11-07-2016
I wonder if your idea makes sense at all. According to man mpstat the reported values are average since the system boot - unless you give an interval.
And if you want the average over all CPUs, consider vmstat or iostat -c (again, with an interval).
# 6  
Old 11-07-2016
Looks like my bash shell does not have support for associative arrays ?

Code:
 
# more a.sh 
#!/bin/bash

declare -A cpuusage
{ read
  read
  read -a HD
  while read -a TMP
    do for i in ${!HD[@]}
         do cpuusage[${TMP[1]}","${HD[$i]}]=${TMP[$i]}
         done
    done
} < x
echo ${cpuusage[2,%soft]}
#
# more x
Linux 3.0.101-0.15.1.6550.0.PTF-default (SC-1)  11/08/16        _x86_64_

09:30:09     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
09:30:09     all    0.07    0.00    0.09    0.00    0.00    0.02    0.00    0.00   99.82
09:30:09       0    0.14    0.00    0.09    0.00    0.00    0.02    0.00    0.00   99.75
09:30:09       1    0.07    0.00    0.07    0.00    0.00    0.01    0.00    0.00   99.85
09:30:09       2    0.07    0.00    0.12    0.00    0.00    0.04    0.00    0.00   99.77
09:30:09       3    0.22    0.00    0.20    0.00    0.00    0.05    0.00    0.00   99.53
09:30:09       4    0.12    0.00    0.15    0.00    0.00    0.01    0.00    0.00   99.72
09:30:09       5    0.11    0.00    0.18    0.00    0.00    0.02    0.00    0.00   99.69
09:30:09       6    0.08    0.00    0.04    0.00    0.00    0.00    0.00    0.00   99.88
09:30:09       7    0.07    0.00    0.05    0.00    0.00    0.00    0.00    0.00   99.88
09:30:09       8    0.07    0.00    0.04    0.00    0.00    0.00    0.00    0.00   99.89
09:30:09       9    0.12    0.00    0.05    0.00    0.00    0.00    0.00    0.00   99.83
09:30:09      10    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
09:30:09      11    0.03    0.00    0.09    0.00    0.00    0.04    0.00    0.00   99.85
09:30:09      12    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.94
09:30:09      13    0.07    0.00    0.03    0.00    0.00    0.00    0.00    0.00   99.90
09:30:09      14    0.03    0.00    0.40    0.00    0.00    0.18    0.00    0.00   99.39
09:30:09      15    0.05    0.00    0.10    0.00    0.00    0.00    0.00    0.00   99.84
09:30:09      16    0.07    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.91
09:30:09      17    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
09:30:09      18    0.02    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
09:30:09      19    0.03    0.00    0.02    0.00    0.00    0.00    0.00    0.00   99.95
#
#
#./a.sh 
./a.sh: line 3: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
./a.sh: line 9: all,09: value too great for base (error token is "09")
./a.sh: line 10: 2,%soft: syntax error: operand expected (error token is "%soft")
#


Last edited by sshark; 11-07-2016 at 10:03 PM..
# 7  
Old 11-07-2016
Associative arrays are as of bash 4 . If that is not available on your system, you could try ksh93 or zsh with a bit of syntax adjustment (typeset instead of declare, read -A instead of read -a, ...)

Code:
$ bash -c 'declare -A cpuusage; echo $?'
bash: line 0: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
2
$ bash4 -c 'declare -A cpuusage; echo $?'
0
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi Dimensional array

I have an array of names. Each one of the name, has a number represented to it. For example A has an ID 8, B has an ID 2. What I am after is a for loop that when the array is in position 1, a particular variable is set to the value of position 1 in array 2 declare -a arr=("A" "B" "C"... (6 Replies)
Discussion started by: nms
6 Replies

2. Shell Programming and Scripting

Store in a 2 dimensional array - Perl

Hey guyz. Here is my sample input file following by first part of my code: * A B C D E reg1 1 0 1 1 0 reg2 0 1 0 0 1 reg3 1 0 0 1 0 reg4 0 0 1 0 1 reg5 1 1 0 0 1 use strict; use warnings; open (IN, "test_input.txt") or die ("Can't open file.txt: $!\n"); my $line = <IN>; ... (2 Replies)
Discussion started by: @man
2 Replies

3. Programming

Return two dimensional array in c++

I am writing matrix multiplication and trying to return a two dimensional array from a function but I keep getting errors. Can someone please help me? here is my code (it is just the skeleton of my program): void main () { ... int *matmultiply (int, int, int, int , int , int ) ... } ... (4 Replies)
Discussion started by: saboture88
4 Replies

4. Programming

Passing two dimensional array to a function

Hi. I have a problem with passing two dimensional array to a function. First, let me show my code to explain what i am going to do: I have function:void initialize_board(char board);which is supposed to modify content of passed array. I have read here: Question 6.18 how such arrays should be... (3 Replies)
Discussion started by: Shang
3 Replies

5. UNIX for Dummies Questions & Answers

Help: stdin to multi-dimensional array

I cant get out of this while loop at the beginning of my program. Just reading from stdin one char at a time and storing it into a multi-array. Need to fix it with in two hours. #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include... (1 Reply)
Discussion started by: unt_engn
1 Replies

6. Shell Programming and Scripting

sorting multi dimensional array

Hi there, Can someone let me know how to sort the 2 dimensional array below by column 1 then by column 2? 22 55 2222 2230 33 66 44 58 222 240 11 25 22 60 33 45 output: 11 25 22 55 22 60 33 45 33 66 44 58 (6 Replies)
Discussion started by: phoeberunner
6 Replies

7. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

8. Shell Programming and Scripting

2 dimensional array in unix

I am trying to implementing two dimensinal array in ksh script.Would you pls help me out. I have a large size of file, File contains looks like ID SID VLAUE1 VALUE2 TOTALVALUE 1 a1 01 02 03 1 b1 02 05 07 ... (2 Replies)
Discussion started by: pritish.sas
2 Replies

9. Shell Programming and Scripting

Help for record (2 dimensional array.)

I am going to develop a address book using the shell scripting commands without sed, awk, .... I am thinking to apply the concept of 2 dimenstional array. Can I create a two dimensional array for the insertion/updation/deletion of record in unix. If yes then tell me plz or recommend me some... (1 Reply)
Discussion started by: murtaza
1 Replies

10. Shell Programming and Scripting

Reference two dimensional array in Perl sub

I am trying to reference a two dimensional array in a subroutine and can't seem to figure this one out in Perl. Does anybody know? Please enlighten me. #!/usr/bin/perl -w use constant DIM => 4; sub Shift_elements_right{ my (@Input, @Output) = @_; for ($i = 0 ; $i <= DIM ;... (5 Replies)
Discussion started by: photon
5 Replies
Login or Register to Ask a Question