0

I'm trying to apply an existing solution [1] to my use case but it's not working as expected. Here we go: I'm extracting log errors from a file:
grep ERROR log.txt
ERROR line1
ERROR line3

Now, if I try to store the output in an array:

targets=($(grep "ERROR" log.txt))
length=${#targets[@]}

for ((i = 0; i != length; i++)); do
    echo "target $i: '${targets[i]}'"
done

Then, I get the following output:

target 0: 'ERROR'
target 1: 'line1'
target 2: 'ERROR'
target 3: 'line3'

Why the output is different from what I'd get with basic grep to standard output ? Can you help me to spot what is wrong with it?

[1] Store grep output in an array

Carla
  • 2,566
  • 3
  • 26
  • 51

0 Answers0