1

I am reading a file in a loop and there is string i got in each iteration. I need to add this element to array but in new line

i use the blow cmd ARRAYNAME+=$ELEMENTNAME

but what happens thet element is added to the last one but separated by " " instead of lines

while IFS='' read -r line || [[-n "$line" ]];do
MAPPING=`echo $line | grep -e session -e sub | cut -d: -f3 | grep -i ","`
MAPPINGMAT+=("$MAPPING")
echo ${MAPPINGMAT[*]}
done < "$1"

output

1
1 2
1 2 3

but i need it to be

1
2
3
7
  • The line where you're assigning to MAPPING isn't valid I don't think, since grep -i needs a pattern, then you're not appending a full line to your array, and you're telling the loop to print the entire line on a single line in each iteration of the loop. Use the for loop from my answer after the while loop to get it to print each element on its own line Commented Aug 23, 2015 at 13:24
  • grep -i is updated i missed it, i think for shall work but it will do much processing in big files as there is already while loop so i am trying to avoid it Commented Aug 23, 2015 at 13:29
  • If you print the entire array on a single line in every iteration of the loop, you're going to get the output you reported, there's no way to get different output unless you change how you're printing it. The way you're adding it to the array isn't part of the problem for your output, the way you're producing the output is. If you don't want a separate loop to print the array afterward, you could just echo $MAPPING within the while instead of printing the whole array each time Commented Aug 23, 2015 at 13:31
  • what you are saying is what i did. echo $MAPPING is done within while loop. i need only to add the output of MAPPING in a separate line in an array. reading the array shall be outside the while loop, here is just for tracking Commented Aug 23, 2015 at 13:38
  • So what about either my answer or deepmax's answer isn't working then? If you're iterating over the array elements there isn't a concept of "lines" there, there are just elements, you can insert the lines where you like around the elements. Both our answers sound like they're doing what you're asking for to me Commented Aug 23, 2015 at 13:42

2 Answers 2

2

Use parenthesizes:

ARRAYNAME+=("$ELEMENTNAME")

To append an item to the end of the ARRAYNAME.

The reason you see below output

1
1 2
1 2 3

is that you're print the whole array in the loop in each iteration. Therefore echo ${MAPPINGMAT[*]} should not be in the loop. Replace it by a simple echo "$MAPPING".

Sign up to request clarification or add additional context in comments.

Comments

1

With parens and quotes I get the entire line as an array element like so:

declare -a arr
while read -r line; do
    arr+=("$line")
done < input

Then I get each line in arr as seen in:

for(( i=0; i<${#arr[@]}; i++ )); do
    echo ${arr[$i]}
done

3 Comments

my script as below while IFS='' read -r line || [[-n "$line" ]];do MAPPING=echo $line | grep -e session -e sub | cut -d: -f3 | grep -i MAPPINGMAT+=("$MAPPING") echo ${MAPPINGMAT[*]} done < "$1"
It's very hard to follow that script in a comment, could you edit your question to include it?
In bash 4, this loop can be replaced with readarray -t arr < input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.