bash grep results into array

后端 未结 2 424
无人及你
无人及你 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:08
     readarray GREPPED < <(grep "some expression" index)
     for item in "${GREPPED[@]}"
     do
         # echo
         echo "${item}"   
     done
    

    Oh, and combine those -v greps like so:

     egrep -v '\.(jpg|gif|xml|zip|asp|php|pdf|rar|cgi|html?)'
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题