0

In the below code, the array length is 1.

Could anyone explain why, as grep output will displayed in each new line but when it is stored in the array, the array length will be 1.

How to display each line reading the array?

#!/bin/bash

NUM=()
SHORT_TEXT=()
LONG_TEXT=()

#cat /tmp/dummy2 | 
while read NUM 
do
    LONG_TEXT+=$(grep $NUM -A4 RtpLogShm.Msg | grep -vi abate | grep ^LG)
    done < /tmp/dummy2

    #cat /tmp/dummy1 | 
    while read LINE
    do
        NUM+=$(echo $LINE | awk -F':' '{print $1}')
        SHORT_TEXT+=$(echo $LINE | awk -F':' '{print $2}')
        done < /tmp/dummy1

        printf "[%s]\n" "${LONG_TEXT[@]}"
    done
done
jww
  • 97,681
  • 90
  • 411
  • 885
Nikhil
  • 576
  • 1
  • 11
  • 31
  • You are not creating the LONG_TEST as an array - See this for how to store the results as an array - http://stackoverflow.com/questions/24890764/store-grep-output-in-array-bash – rahul Apr 14 '15 at 07:49

1 Answers1

2

In bash, the syntax of appending to an array is (say we want to append an element stored in ${new_element} to an existing array ${array[@]}):

array=("${array[@]}" "${new_element}")
4ae1e1
  • 7,228
  • 8
  • 44
  • 77