Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed with an array

Tags:

bash

sed

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
like image 831
Max Avatar asked Oct 18 '25 10:10

Max


1 Answers

Because the sed script is in single quotes, which prevents all expansion:

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.

like image 177
Lev Levitsky Avatar answered Oct 19 '25 23:10

Lev Levitsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!