This question already has an answer here:

Is it possible to use sed to replace the entire example bracketed line below? I seem to be running into trouble finding out a delimiter to use since / and many other special characters exist in the actual line I want to replace.

Line needing replacing:

This is a random example since the actual line I want to use is much less friendly to the eyes.


If there is any tool better suited for this please let me know. Any help would be greatly appreciated.

marked as duplicate by don_crissti, HalosGhost, techraf, G-Man, GAD3R Nov 8 '16 at 7:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


If you want to replace the entire line only, you can use a simple workaround in a similar fashion to this answer.

Here is your line:


The workaround is, print this line before the rest of the file, then use Sed to change any lines which entirely consist of an exact match of the first line.

{ printf '%s\n' '<example>=(a,b,c);{!@#$%^&*?/};</example>'; cat somefile;} |
  sed -e '1h;1d;G;s/^\(.*\)\n\1$/text to change to/;t' -e 's/\n.*$//'

However—you should learn which characters need escaping in a Sed regex, anyway.

Not the answer you're looking for? Browse other questions tagged or ask your own question.