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/;'
to no avail. Or could it be bash that is the culprit?
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 {
# $1 is file
line=`grep -nm 1 $findstring $1 | cut -s -d: -f1`
text=`grep -m 1 $findstring $1`
pre=`echo $text | sed -e "s/^\(.*\)$findstring.*$/\1/;"`
post=`echo $text | sed -e "s/^.*$findstring\(.*\)$/\1/;" -e 's/\\n/\\\\n/;'`
echo "text |$text| pre |$pre| post |$post| line |$line|"
head -n$(($line-1)) $1 > $1.tmp
sed -e "s/^/$pre/g;" -e "s/$/$post/g;" < $newDfile >> $1.tmp
tail -n+$(($line + 8)) $1 >> $1.tmp #original text is 8 lines
mv -f $1.tmp $1
}
When $post is printed by echo, it shows the \n - but by the time the file is on disk, it becomes a newline. What should I do to ensure that it stays as the characters
\n?