如果执行带有多行的grep命令,并且每行有多个单词,则输出似乎按字而不是按行存储在数组中。如何更改它以便每行存储?
例如:
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]}
如果这返回10行,并且第一行读取“这是一行”
它只输出“This”
答案 0 :(得分:3)
您可以使用IFS更改字段分隔符:
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
答案 1 :(得分:0)
如果要添加带空格的元素,则需要引用它,如下例所示:
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
所以在你的情况下,尝试类似的东西:
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]}"