0

My need is to print a range of array elements, minus 20 and plus 20 elements from the point "string" is found using KSH93.

I have tried many iterations of code and read many links for example, How do I iterate over a range of numbers defined by variables in Bash?

/usr/bin/ksh93 -c 'mdm=(`/usr/sbin/mdmprpt 2>/dev/null`);
for index in "${!mdm[@]}"; do
    if [[ ${mdm[$index]} =~ Fault.? ]]; then
        i=${mdm[$index]};
        for x in {1..$i}; do 
            echo $x
        done
     fi
done

actual result is {1..(Faulting}

when it should print 20 lines before and or after of index 52. Ideally both.

__Raw Data__from_sample_code

mdm[32] is 6400000000000000

mdm[33] is 0000000000000000

mdm[34] is 0000000000000000

mdm[35] is 0000000000000000

mdm[36] is 00000000

mdm[37] is Symptom

mdm[38] is Information:

mdm[39] is Crash

mdm[40] is Location:

mdm[41] is [000000000010D614]

mdm[42] is IPRA.$ha_critic+114

mdm[43] is Component:

mdm[44] is COMP

mdm[45] is Exception

mdm[46] is Type:

mdm[47] is 131

mdm[48] is Data

mdm[49] is From

mdm[50] is CPU

mdm[51] is #8

mdm[52] is (Faulting

mdm[53] is CPU)

mdm[54] is backup_files

mdm[55] is cfgbackups

mdm[56] is config

mdm[57] is install.log

mdm[58] is ioscli.log

mdm[59] is pager.trace

mdm[60] is rules

mdm[61] is smit.log

mdm[62] is smit.script

mdm[63] is smit.transaction

mdm[64] is snap.pax.Z

mdm[65] is MST

mdm[66] is State:

mdm[67] is R0:

mdm[68] is 0000000000050FB4

mdm[69] is R1:

mdm[70] is F00000002FF471D0

mdm[71] is R2:

mdm[72] is 00000000038B6110

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
dirman
  • 61
  • 5

2 Answers2

1

When you find the matching string you also have its (numerical) index (${index}), so just +/-20 to ${index} to get the desired range.

We'll also need some additional logic to make sure our desired range of indexes falls within the range of available indexes. Keep in mind that for an array with 'n' records the available index range will be '0 to (n-1)'.

for index in "${!mdm[@]}"
do
    if [[ ${mdm[$index]} =~ Fault.? ]]
    then
        start=$((index-20))
        end=$((index+20))

        # if 'start' is less than 0 then reset it to 0

        [ ${start} -lt 0 ] && start=0

        for x in $( seq ${start} ${end} )
        do
            # break if we run out of array elements

            [ "${mdm[${x}]:-undefined}" = 'undefined' ] && break

            # display our numeric index and contents of associated array item

            echo "${x} : ${mdm[${x}]}"
        done

        break
    fi
done

I created a data file with 32 initial lines of 'XXXXXX', the 41 lines of sample data from the question, and an additional dozen lines of 'XXXXXX' at the end of the file; I then ran the above code snippet against the file and generated:

32 : 6400000000000000
33 : 0000000000000000
34 : 0000000000000000
35 : 0000000000000000
36 : 00000000
37 : Symptom
38 : Information:
39 : Crash
40 : Location:
41 : [000000000010D614]
42 : IPRA.$ha_critic+114
43 : Component:
44 : COMP
45 : Exception
46 : Type:
47 : 131
48 : Data
49 : From
50 : CPU
51 : #8
52 : (Faulting
53 : CPU)
54 : backup_files
55 : cfgbackups
56 : config
57 : install.log
58 : ioscli.log
59 : pager.trace
60 : rules
61 : smit.log
62 : smit.script
63 : smit.transaction
64 : snap.pax.Z
65 : MST
66 : State:
67 : R0:
68 : 0000000000050FB4
69 : R1:
70 : F00000002FF471D0
71 : R2:
72 : 00000000038B6110
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • i get it now, but my particular version of ksh93 does not recognize the seq instruction. thank you! – dirman Sep 09 '19 at 13:32
  • apologies. I wrote the example under bash; you should be able to replace the `for` clause with something like: `for ((x=${start};x<=${end};x++))` – markp-fuso Sep 09 '19 at 13:39
0

Well I could just use a bunch of prints, which does do what I want, but Id think there would be a range operator I have yet to get to work.

    /usr/bin/ksh93 -c 'mdm=(`/usr/sbin/mdmprpt 2>/dev/null`);
    for index in "${!mdm[@]}"; do
    if [[ ${mdm[$index]} =~ Fault.? ]]; then
    print ${mdm[$index-20]}
     print ${mdm[$index-19]}
    print ${mdm[$index-18]}
    print ${mdm[$index-17]}
    print ${mdm[$index-16]}
    print ${mdm[$index-15]}
    print ${mdm[$index-14]}
    print ${mdm[$index-13]}
    print ${mdm[$index-12]}
    print ${mdm[$index-11]}
    print ${mdm[$index-10]}
    print ${mdm[$index-9]}
    print ${mdm[$index-8]}
    print ${mdm[$index-7]}
    print ${mdm[$index-6]}
    print ${mdm[$index-5]}
    print ${mdm[$index-4]}
    print ${mdm[$index-3]}
    print ${mdm[$index-2]}
    print ${mdm[$index-1]}
    print ${mdm[$index]}
    fi
    done'

6400000000000000

0000000000000000

0000000000000000

0000000000000000

00000000

Symptom

Information:

Crash

Location:

[000000000010D614]

IPRA.$ha_critic+114

Component:

COMP

Exception

Type:

131

Data

From

CPU

8

(Faulting

Community
  • 1
  • 1
dirman
  • 61
  • 5
  • really hard to read the output. You obviously know how to format the code portions, why not use the same technique on your output? Use the `{}` tool from the edit menu on selected text for proper code/data/errMsg formatting. .... – shellter Sep 05 '19 at 20:48