bash grep results into array

后端 未结 2 423
无人及你
无人及你 2021-01-11 13:57

In bash I\'m trying to collect my grep results in array, each cell holding each line. I\'m downloaing urls with this line

wget -O index -E $CurrentURL

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-11 14:11

    Probably most elegant among several poor alternatives would be to use a temp file.

    wget $blah | grep 'whatever' > $TMPFILE
    declare -a arr
    declare -i i=0
    while read; do
        arr[$i]="$REPLY"
        ((i = i + 1))
    done < $TMPFILE
    

    I don't have time to explain why, but do not pipe directly into read.

    No Unix shell is an appropriate tool for this task. Perl, Groovy, Java, Python... lots of languages could handle this elegantly, but none of the Unix shells.

提交回复
热议问题