1

I am struggling around with awk and want to save the awk output separated by the delimiter to array. So just to have this working:

array[index]=$line

with $line equal value between separator '[[:blank:]]{2,}' in the desired file.

I tried already multiple solutions like:

myarr=$(/usr/xpg4/bin/awk -F '[[:blank:]]{2,}' 'FNR > 4 { for (i=1; i<=NF; i++) if ($i != "") print $i;}' my-file)

or

array=()
counter=0
/usr/xpg4/bin/awk -F '[[:blank:]]{2,}' 'FNR > 4 { for (i=1; i<=NF; i++) if ($i != "") print $i;}' my-file | while read -r line; do
    array[counter]=$line
    counter=$((counter+1))
done

echo ${#array[@]}
echo ${array[1]}

none of them produce the desired result. But when I modify the second case and echo the $line and the $counter I get the array, but it is not the case for a large file, I just do not want to output the whole lines to the console to get my array filled up - it makes no sense for me:

This works -> but uggly/bad performance for large files

array=()
counter=0
/usr/xpg4/bin/awk -F '[[:blank:]]{2,}' 'FNR > 4 { for (i=1; i<=NF; i++) if ($i != "") print $i;}'my-file | while read -r line; do
    echo $line
    echo $counter
    array[counter]=$line
    counter=$((counter+1))
done

echo ${#array[@]}
echo ${array[1]}
Inian
  • 80,270
  • 14
  • 142
  • 161
N.Zukowski
  • 600
  • 1
  • 12
  • 31
  • Marking as duplicate do not make sense, because I just didn't realize what the problem was, so I could not find the answer using search. – N.Zukowski Sep 08 '17 at 13:35
  • Sirion: It is not a problem. It is kind of a way to _protect_ the question from welcoming other incorrect answers and letting know that this was the problem – Inian Sep 08 '17 at 13:37

1 Answers1

1

BashFAQ/024 - I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?

Bash pipe-lines create a sub-shell in which the variables updated are not reflected at all. In your case either of your array array and counter variable scopes are lost soon after the sub-shell terminates. Use process-substitution(<( … )) in bash as below

Also the array subscript you are using is incorrect; which should have been done as

array[$counter]="$line"

Combining the two,

while read -r line; do
    echo "$line"
    echo "$counter"
    array[$counter]="$line"
    counter=$((counter+1))
done < <(/usr/xpg4/bin/awk -F '[[:blank:]]{2,}' 'FNR > 4 { for (i=1; i<=NF; i++) if ($i != "") print $i;}' my-file)
Inian
  • 80,270
  • 14
  • 142
  • 161