Need help to grep/awk into array

Hey guys,

First post, I'm not a UNIX script guru and am trying to hack together something as a requirement ASAP.

I have scanned the forums and Googled for this and have found similar issues but no usable solutions so I am hoping one of you guys can help me out.

Basically I have a batch file filled with data -- a mock up example is below:

IMAHDR FOO BAR FWEEP 000000
row
row
...
IMATRL FOO BAR FWEEP 000024
IMAHDR FOO BAR FWEEP 000000
row
row
...
IMATRL FOO BAR FWEEP 000008
IMAHDR FOO BAR FWEEP 000000
row
row
...
IMATRL FOO BAR FWEEP 000234

Amongst some other things, I need to extract all instance of lines that are "trailer" records which are indicated with "IMATRL".

I can do that quite easily with grepping. However, in my eventual processings script, I need to get all trailer records out into an array so that I can then use them as a basis of analysing the file for statistical purposes (matching trade data passed in vs what is processed out, etc)

My problem is that no matter what I do, I simply cannot get the "whole line" extracted out into an array. I've tried various playings with 'awk' but it hasn't worked:

`grep ${TRAILER_ID} ${file}|awk 'BEGIN {FS="IMATRL"} {print $1 }'`
or
`grep ${TRAILER_ID} ${file}|awk 'BEGIN {FS="\n"} {print $1 }'`

So far, this is my current test code:

#!/bin/ksh

TRAILER_ID=IMATRL
file=xxx.txt

set -A y `grep ${TRAILER_ID} ${file}`

i=0
while [[ $i -lt ${#y
[*]} ]] ; do
    echo "y[$i] = ${y[$i]}"
    ((i=$i+1))
done

But it dumps out everything to invidual elements :

y[0] = IMATRL
y[1] = FOO
y[2] = BAR
y[3] = FWEEP
y[4] = 000024
y[5] = IMATRL
y[6] = FOO
y[7] = BAR
y[8] = FWEEP
y[9] = 000008
y[10] = IMATRL
y[11] = FOO
y[12] = BAR
y[13] = FWEEP
y[14] = 000234

Running the basic grep on the command line prints out the 3 lines with neat little "\n" so it looks like what I'd want. But in script, it obviously resolves the grep result to a single string which is then using a single " " whitespace as the tokeniser for the array creation.

Can anyone help me get my array output to look like this :

y[0]=IMATRL FOO BAR FWEEP 000024
y[1]=IMATRL FOO BAR FWEEP 000008
y[2]=IMATRL FOO BAR FWEEP 000234

And without using a temp file to dump results to before re-parsing?

Thanks guys.

#!/bin/ksh
typeset -i i=0
awk '/^IMATRL/' inputfilename | while read value
do
  arr="$value"
  i=$(( $i + 1) )
done

The limit is 1023 elements per array in ksh.

Thanks a lot Jim - that works a treat!

As for the 1023 limit - that should be fine -- the batch files I have to process should at *most* have maybe 5 to 6 entries that need to be extracted for array purposes and the lines in production are 80chars in length so that's fine.

:b:

might be another way to work around

 
awk '/^IMATRL/ { arr[++i]=$0 } END {for (j in arr) print arr[j] }' rem.txt