2

I have this sting: the string's problem and I want to change this to:

file 'the string'\''s problem'

My code looks like this:

newline=`echo "$string" | sed "s/'/'\\\''/g"`
echo $newline | sed "s|$newline|file '$newline'|g" >> "$newPList"

But the result from this is:

file 'the string'''s problem'

Any suggestions?

2 Answers 2

3

Not so complicated

$ str="string's problem"
$ sed 's/'\''/&\\&&/g' <<<"$str"
string'\''s problem
$ new="file '$(sed 's/'\''/&\\&&/g' <<<"$str")'"
$ echo "$new"
file 'string'\''s problem'

It is critically important to quote the "$new" variable when you use it.

3
  • Thank you! I saw now that my edited way has not work. Jul 15, 2015 at 16:13
  • I see you've never accepted any answer to any of your questions. Please read askubuntu.com/help/someone-answers and learn how the community works. Jul 15, 2015 at 16:22
  • Sorry, I will take care of it in the future. Jul 15, 2015 at 16:44
3

It's a perl one-liner, all in one, without additionally string constructions ;)

$ perl -pe "s/^(.*)'(.*)$/file '\$1\'\\\'\'\$2'/" <<< "the string's problem"
file 'the string'\''s problem'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .