Reputation: 2347
If you perform a grep command with multiple lines, and multiple words per line, the output seems to be stored in an array by word rather than by line. How can you change it so that it's stored per line?
For example:
first_title=( $(egrep -o 'class\=\"title\" title\=\"'.\{1,80\} index.html
| egrep -o title\=\".*\"\> | sed 's/title\=\"//g' | sed 's/">//g') )
echo ${first_title[0]}
If this returned 10 lines, and the first read "This is a line"
It would only output "This"
Upvotes: 2
Views: 1669
Reputation: 64308
You can use IFS to change the field separator:
IFS='
'
first_title=( $(egrep -o 'class\=\"title\" title\=\"'.\{1,80\} index.html
| egrep -o title\=\".*\"\> | sed 's/title\=\"//g' | sed 's/">//g') )
echo ${first_title[0]}
unset IFS
Upvotes: 3
Reputation: 185035
If you want to add an element with spaces, then you need to quote it like in the following example :
arr=( "this is a line" "this is another line" this is some words )
echo "${arr[0]}"
this is a line
printf '%s\n' "${arr[@]}"
this is a line
this is another line
this
is
some
words
So in your case, try something like that :
first_title=(
$(
egrep -o 'class\=\"title\" title\=\"'.\{1,80\} index.html |
egrep -o title\=\".*\"\> |
sed 's/title\=\"//g;
s/">//g;
s/.*/"&"/'
)
)
echo "${first_title[0]}"
Upvotes: 0