On Thursday, December 23, 2010 11:28:37 am you wrote:
> OK, I'm going to forgo Rexx and learn bash script!
> I want to input a file into an array. For instance I want the variable xyz
> to have the contents of /tmp/test. /tmp/test looks like:
>
> 08:50:01 AM       all      3.48      0.00      0.18      0.15      0.19
> 95.99
> 09:00:02 AM       all      3.51      0.00      0.19      0.15      0.11
> 96.05
>
>
> I tried:
>
> xyz=(`cat /tmp/test`)
>   and
> xyz=('grep all /tmp/test`)
>
> but I only get the first word, the 08:50:01. How can I get everything?

I agree with Phil: you probably want to use something other than bash arrays
because they don't scale.  If I really wanted to read everything into an
array, I'd do it in awk, which can do multi-dimensional arrays.  Or in PERL.

But you can do this all in bash without resorting to arrays.  You usually need
to process each line (or record) of the input sequentially, perhaps collecting
information such as sums as you go.  Here's an example of how to do that:

NumRecs=0
while read TIME KWD VAL1 VAL2 VAL3 VAL4 VAL5 VAL6
do      NumRecs=$((NumRecs+1))
        ...
done < /tmp/test

I just noticed a big problem with using bash to process your input: bash
doesn't handle floating-point numbers, only integers.  If you want to do
anything with those floating point values, you'll either have to convert them
to fixed-point integers (by multiplying them all by 100, for example), or hand
them off to some other tool that handles floating-point.  If you're sending
them to another tool, then you wouldn't want to use the loop above because
you'd be invoking that other program for each record on the input.  You could
do something useful with the numbers in awk, like this:

awk 'BEGIN{min=10000; max=0; sum=0} \
        sum += $3; if ($3 < min) {min=$3; mintime=$1}; \
        if ($3 > max) {max =$3; maxtime=$1}; \
        }
        END {print "Minumum:", min, "at", mintime; \
                print "Maximum:", max, "at", maxtime; \
                print "Average:", sum / NR; \
        }'  /tmp/test

That will output the minimum, maximum and average values of the third column,
along with the most recent times the min and max occurred.  Just a simple
example of how to process record like this in awk.
        - MacK.
-----
Edmund R. MacKenty
Software Architect
Rocket Software
275 Grove Street  -  Newton, MA 02466-2272  -  USA
Tel: +1.617.614.4321
Email: [email protected]
Web: www.rocketsoftware.com

----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390
----------------------------------------------------------------------
For more information on Linux on System z, visit
http://wiki.linuxvm.org/

Reply via email to