-1

I've been trying all sorts of variations in this sed command in order to insert a linebreak in a replacement string, and also to match a multiple line string that I wish to replace. But when I try, it either has no effect or creates an ^M when using \n or \r.

I'm using GNU sed with zsh:

find ./ -type f -exec sed -i "s/<?php echo \$var_one;?>/<?php echo \$var_one;?>\n\n/g" {} \;

find ./ -type f -exec sed -i 's/<?php echo \$var_one;?>/<?php echo \$var_one;?>\n\n/g' {} \;

Is there no way to do this with sed, or am I doing something wrong?

Dogbert
  • 77
  • 1
  • 8

1 Answers1

0

I am assuming you have in a file

<?php echo $var_one;?>/<?php echo $var_one;?>

that is $var instead of \$var

find -type f  -exec sed 's#\(<?php echo \$var_one;?>/<?php echo \$var_one;?>\)#\1\n#g' {} \;

replace you double quotes with single quotes surrounding your sed statment

Use another delimiter for example I use #. You will reiceve an error if you use / because / is in your pattern.

repzero
  • 8,254
  • 2
  • 18
  • 40
  • I don't understand. What exactly validates the newline? I assume there's only one in your example. And in the file, I have var_one once, but I want to replace it with itself and a line break or two. – Dogbert Nov 13 '15 at 05:08
  • a newline is a metacharacter and it is recognised by sed. if you want more than one new line replace "\1\n" with "\1\n\n"..okay...since you have a variable to be subsituted..then replace the single quotes with double..the reason why i use single quotes is becuase certain characters are interpretated by the shell instead of sed..it appears to be fine on my side – repzero Nov 13 '15 at 05:13