![]() |
prevent sed from replacing \n with newline
I'm writing a script to replace some text that exists in about 50 .lex, .y, and .cc source code files, sometimes more than once in a file. Sometimes the text is in a multiline C comment, and other times it's within a multiline C string.
I use sed to grab the start and end of each line and wrap the new text in the old whitespace and/or quotes and \n. Problem is, sed is changing the characters \n into a newline. Is there a way to tell sed to not process escape sequences? I tried using several variations of Code:
sed -e 's/\\n/\\\\n/;'I would give up on the script and do it by hand, but this is something that I must do from time to time. Here's the function which replaces the first occurrence found: Code:
function replacetext { |
show some sample input. describe your output.
|
Quote:
Code:
/******************************************************************************I think that in some files the string is printed when the program runs, rather than being printed into a comment in a source code file. HTH |
These work for me using GNU sed in bash
Code:
sed 's/\\n/\\\\n/' # Inside $(.......)This is a more general method of escaping special characters in variables which will be used inside sed. The variables should be inside double quotes when used in the sed expressions so spaces do not need escaping. Code:
# For basic regular expressions used by sed |
Thanks, Kenhelm. I had changed the number of backslashes in the replacement string, but didn't think to change the number in the string being searched for. I just assumed it was working. :o
|
| All times are GMT -5. The time now is 07:49 PM. |