Match Pattern and store next value into array

Hi,

I am trying to write a script which parses a log file and will eventually put the values in an array so that I can perform some math on it. In this file I am only interested in the last 200 lines so here is the command I use to display the contents in a manageable manner.

tail -200 /home/log.txt | grep -A6 'Header' | grep -v 'Header'
 

The output is like this

 
Some numbers and characters 1 = 5.09093e-03 C
Some numbers and characters 2 = 6.09030e-03 C
Some numbers and characters 3 = 4.48998e-03 C
Some numbers and characters 4 = 4.55334e-03 C
Some numbers and characters 5 = 7.38398e-02 C
 

I would like to store the numbers after the = sign into an array cutting off the whitespace in front and the whitespace, C and newline character at the end.

Thanks

Try this...

#If your set supports -A option
set -A arr $(tail -200 /home/log.txt | grep -A6 'Header' | grep -v 'Header' | awk '{printf $(NF-1)" "}')
 
#if not, try this...
i=0
for val in $(tail -200 /home/log.txt | grep -A6 'Header' | grep -v 'Header' | awk '{printf $(NF-1)" "}')
do
  arr[$i]=$val
  ((i=i+1))
done
echo ${arr[0]}
echo ${arr[1]}
...

If you can provide the exact data, may be we can simplify the grep commands for you...

--ahamed

Thanks Ahamed101 it worked great. I could not use the set but I was able to use your second suggestion.
I think the greping is okay since it works, but thanks for offering to help with that.

only thing was that I had to change arr[$i]=$val to arr[i]=$val.

Could you tell me what awk '{printf $(NF-1)" "}' does?

It prints the last-but-one field in a line, followed by a space.