bash 将 sed 与数组一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15052268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using sed with an array
提问by Max
Could anyone tell me why this is not working?
谁能告诉我为什么这不起作用?
Temp=$(sed -n '/"${Arr[$index]}"/,/"${Arr[$((index+1))]}"/p' $Text);
It still does not work. I tried to do this:
它仍然不起作用。我试图这样做:
index=0
while [ "$index" -lt "$((Arr_LEN-1))" ]; do
Temp=$(sed -n "/${Arr[$index]}/,/${Arr[$((index+1))]}/p" $Text);
let "index++"
done
回答by Lev Levitsky
Because the sedscript is in single quotes, which prevents all expansion:
因为sed脚本是在单引号中,这阻止了所有扩展:
Enclosing characters in single quotes preserves the literal value of each character within the quotes.
将字符括在单引号中会保留引号内每个字符的字面值。
Changing single quotes to double quotes should help though:
将单引号更改为双引号应该会有所帮助:
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes.
双引号中的字符会保留引号内所有字符的字面值,但 $、`、\ 和启用历史扩展时的 ! 除外。字符 $ 和 ` 在双引号内保留其特殊含义。
回答by William Pursell
Because you are using single quotes. Try:
因为您使用的是单引号。尝试:
Temp=$(sed -n "/${Arr[$index]}/,/${Arr[$((index+1))]}/p" $Text);

