1

I have the code like this:

sed "s/TEST_CASES_R/$testCaseLocations/g" template >> newfile 

where $testCaseLocations has ,tests/test/,tests/test/2. So this line is failing like:

bad flag in substitute command

How can I solve this?

1

2 Answers 2

3

Ah, sed code injection. What sed sees is

sed "s/TEST_CASES_R/,tests/test/,tests/test/2/g" template >> newfile 

...which is nonsensical. The root problem is that sed cannot differentiate between the things you want it to see as data -- the contents of $testCaseLocations -- and instructions.

The best solution in my opinion is to use awk:

awk -v replacement="$testCaseLocations" '{ gsub(/TEST_CASES_R/, replacement); print }' template >> newfile 

because this neatly sidesteps code injection problems by not treating testCaseLocations as code. You could, in this particular case, also use a different delimiter for sed, such as

 sed "s@TEST_CASES_R@$testCaseLocations@g" template >> newfile 

but then you'd run into trouble if $testCaseLocations contains a @, or if it contains a character that has meaning for sed in the context where it appears, such as \ or &.

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

2 Comments

Thanks for awk solution. Neat one.
Gsub defaults to $0 so the last arg is not needed.
2

Just use another separator for sed, otherwise it sees so many slashes around: sed 's#hello#bye#g' is fine.

In your case:

sed "s#TEST_CASES_R#$testCaseLocations#g" template >> newfile 

See another test:

$ var="/hello" $ echo "test" | sed "s/test/$var/g" sed: -e expression #1, char 9: unknown option to `s' $ echo "test" | sed "s#test#$var#g" /hello 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.