ksh insert element in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh insert element in array
# 8  
Old 04-23-2012
All,

Thank you all for your prompt responses. Tomorrow I will be able to test what you've suggested and get back to you with what worked.
# 9  
Old 04-24-2012
A different approach that I had prepared in case you show your doing 1st - nevertheless - maybe helpful too. Single steps; the test for 11 shouldn't be the problem:
Code:
# cat blafile
# set -A arrayA a b c d e f g h i j g
# echo ${arrayA[*]}
a b c d e f g h i j g
# echo ${#arrayA[*]}
11
# TMP=$(echo ${arrayA[*]}| awk '{$5=0 FS $5; print}')
# set -A arrayA $(echo $TMP)
# echo ${arrayA[*]}| tr -s " " ","
a,b,c,d,0,e,f,g,h,i,j,g

This User Gave Thanks to zaxxon For This Post:
# 10  
Old 04-25-2012
All,

I decided to go with zaxxon's approach. Below is how the code looks now. It almost does what I want which is to get the numbers out of top and create statistics that give better view over time. But for some reason the line below excludes the CPU states from the output of top. I'll be looking into that and also a better way to report top ten cpu, time, size, res to make it easier to read instead of numbers. Thank you all for your help.

Code:
$tp -n 0 > $gnTmp

Below is how $gnTmp looks like, CPU stats line missing.

Code:
load averages:  4.03,  3.95,  3.79    03:49:09
1179 processes:1041 sleeping, 134 zombie, 4 on cpu

Memory: 32G real, 16G free, 19G swap in use, 14G swap free

Code:
wa=/some_path/top
log=/some_path/top/log.txt
tp=/path_to_top/top

MONTH=`date '+%B'`
DAY=`date '+%d'`
YEAR=`date '+%Y'`
TIME=`date '+%X'`
cpTmp=$wa/cpu.tmp
tmTmp=$wa/time.tmp
szTmp=$wa/size.tmp
rsTmp=$wa/res.tmp
gnTmp=$wa/gen.tmp
ovOut=$wa/overview.out
cpu=$wa/topCPU.out
tme=$wa/topTime.out
siz=$wa/topSize.out
res=$wa/topRes.out

### Gathering top output
$tp -n 0 > $gnTmp
$tp -o cpu -n 10 > $cpTmp
$tp -o time -n 10 > $tmTmp
$tp -o size -n 10 > $szTmp
$tp -o res -n 10 > $rsTmp

### Processing and building output
ov=`sed 's/[A-Za-z ]*//g;s/[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}//g;s/^://;s/:/,/;/^$/d' $gnTmp | sed -e ':a' -e '$!N;s/\n/,/;ta'`
ut=`uptime | awk '{print $3}'`
bt=`who -b | awk '{print $4$5,$6}'`

### Create an array out of ov
typeset IFS=,
set -A Ovarray $( print "${ov}" )
set | grep Ovarray

### Test the length of array
Array_length=${#Ovarray[*]}
#echo "Array length is $Array_length"

if [ $Array_length -eq 11 ];
        then
        #       print "Then Array length is 11"
        #       print ${Ovarray[*]}
        #       print ${#Ovarray[*]}
                TMP=$(print ${Ovarray[*]}| awk '{$5=0 FS $5; print}')
                set -A Ovarray $(echo $TMP)
#               print ${Ovarray[*]}| tr -s " " ","
                ov=`print ${Ovarray[*]}| tr -s " " ","`
        else
                print "Else " >> $log
                print $Array_length >> $log
fi

echo "$ut,$ov,$bt,$YEAR,$MONTH,$DAY,$TIME" >> $ovOut

for row in `awk 'NR > 7 {print $1, $10}' $cpTmp`
do
echo "$row,\c" >> $cpu
done
printf "$YEAR,$MONTH,$DAY,$TIME\n" >> $cpu

for row in `awk 'NR > 7 {print $1, $9}' $tmTmp`
do
echo "$row,\c" >> $tme
done
printf "$YEAR,$MONTH,$DAY,$TIME\n" >> $tme

for row in `awk 'NR > 7 {print $1, $6}' $szTmp`
do
echo "$row,\c" >> $siz
done
printf "$YEAR,$MONTH,$DAY,$TIME\n" >> $siz

for row in `awk 'NR > 7 {print $1, $7}' $rsTmp`
do
echo "$row,\c" >> $res
done
printf "$YEAR,$MONTH,$DAY,$TIME\n" >> $res

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to insert a CSV within xml element tag using Python?

Hi Team, I have a CSV file which I have to read through and needs to insert the content within an XML file using Python ONLY ( as most of the code base we have in python only). I managed to find the first part, missing how to insert to XML under "specific" tags. cat input.csv... (0 Replies)
Discussion started by: panyam
0 Replies

2. UNIX for Advanced & Expert Users

Array Element

This question is for someone that's more familiar with Array Element. I need to know if the maximum array element that can be assigned is 1024 and if its so, Is there a workaround solution when the counter exceeded 1024? param_array="$param_nam" counter=$counter+1 #to avoid space... (3 Replies)
Discussion started by: cumeh1624
3 Replies

3. Shell Programming and Scripting

File names as array element in ksh

Hi, I have an ksh array(ARR). the elements to the array are file names. i need to go to each file in the array and manipulate the records. for name in ${files}; do ---this loop is for all the file names in the array for i in $(wc -l < $name); do --this loop is for all the records in... (20 Replies)
Discussion started by: usrrenny
20 Replies

4. Shell Programming and Scripting

How to insert an array element within regex?

Hello to all, I'm trying to separate the string "str" using a regex within match function. The substrings that I want to separate, begin with 22, 23, 24 or 25 and followed by 12 or 14 characters. And I want to replace 22 with MJS, 23 with UYT, 24 with WER and 25 with PIL. For this string... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

5. Shell Programming and Scripting

Not able to call an element from an array in ksh

Hi, I have: # Initialize variables #!/usr/bin/ksh FILENM=$1 INDEX=0 # read filename echo "You are working with the Config file: $FILENM" while read line do echo $line data=$line ((INDEX=INDEX+1)) done <"$FILENM" (3 Replies)
Discussion started by: Marc G
3 Replies

6. Emergency UNIX and Linux Support

Assigning zero to element of ksh array.

set -A matched #find referenced files. for i in ${file_names_html} do counter_j=0 for j in ${file_names_minus_index} do match=`cat $i | grep... (1 Reply)
Discussion started by: robin_simple
1 Replies

7. Shell Programming and Scripting

Problem to initialize ksh array when first element includes hyphen

Hi I'm trying to create an array with variable including hyphen but ksh refuses the first element set -A allArgs set +A allArgs ${allArgs} -all set +A allArgs ${allArgs} -date set +A allArgs ${allArgs} test ./test.ksh: -all: bad option(s) It happens only when first element is like... (4 Replies)
Discussion started by: gdan2000
4 Replies

8. Shell Programming and Scripting

remove an element from array

I need to remove an element from the below array variable TABLENAME. #!/bin/ksh set -A TABLENAME "mirf roxar keke mirs" echo "the array is ${TABLENAME}" If i need to remove say keke and have the final TABLENAME as below, how this could be achieved. Pls throw some light. echo "Modified... (3 Replies)
Discussion started by: michaelrozar17
3 Replies

9. Shell Programming and Scripting

Shift array element

I want to delete and 0th element of array in shell scrpit and also shift all others to one level up. (2 Replies)
Discussion started by: darshakraut
2 Replies

10. Shell Programming and Scripting

Adding array element in KSH

All, I would like to add the first 10 elements of an array. Here is how I am doing it now (only included first few add ops): #!/usr/bin/ksh ###Grab the array values out of a file### TOTAL=`awk '/time/' /tmp/file.out | awk '{print $4}'` set -A times $TOTAL SUM=$((${times} + times... (3 Replies)
Discussion started by: Shoeless_Mike
3 Replies
Login or Register to Ask a Question