13

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

and then i want to grep the 'index' file results (other urls) into array each line per cell, what should be the correct syntax?

Array=(grep "some expression" index)

??

1
  • This is where you need to find the links, correct? so where will links be? It is tough to create a comprehensive Regex for URLs. Commented Nov 20, 2011 at 23:51

2 Answers 2

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

3 Comments

thanks, i'll improve my code with that, the first block of code you wrote me doing what? reading from such array or the first line also puts to array?
The first line outputs to array: gnu.org/s/bash/manual/html_node/Bash-Builtins.html; The other 4 lines just demo how to (whitespace-safely) iterate over them
Note that 'readarray' is a very new addition to Bash. It is available on nearly none of the Linux servers, or even Cygwin and Mingw installations where I work. If you want a portable solution, see my 'Probably most...' post below.
4

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.

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.