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
1

1 Answer 1

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}")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.