0

I tried the following snippet of code, but the sed command replaces with $var explicitly shown rather than as thr value of the var at that time. How can I solve this?s

for var in 01 02
do       
sed -e 's/%\\include{removeMe}/\\include{chapter_$var}/g'
done
1
  • Can you provide a sample of your input data (btw sed command has missing file name). Commented Jan 30, 2014 at 14:36

2 Answers 2

2

Try double quotes:

sed -e "s/%\\include{removeMe}/\\include{chapter_$var}/g"

(Better still, try single/double quotes with a simpler example first.)

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, the solution is to use double quotes to get the variables expanded.
@Beta Using double quotes didn't do any replace! The single quotes did the replace of the text but not the substitution on the $var.
@Beta The problem is the % symbol. Without it it works. But with it, no replace takes place.
add option --posix or escape % with \%
0
for var in 01 02
 do       
   sed -e "/[%]\(\\include{\}removeMe}/ s//\1chapter_${var}}/g"
 done

use the back reference and encapsulation

but it could be replace by if var is not used with a filename association (so sed is a pipe) like in your sample (also because replacement by 01 will remove any possibility of finding a pattern for 02 replacement)

YourStream | sed -e 's/%\(\\include{\}removeMe}/\1chapter_01}/g' 

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.