tags:

views:

55

answers:

2

Hello,

Find:

regexp1 **sometext** regexp2

Replace with:

newregexp1 **sometext** newregexp2

Here, I do not want **sometext** to be modified.
That is to say, if I have the following lines in a file:

Hello somebody! Have a good day.
Hello somebodyelse! Have a good day.

I want the output to be:

Bye somebody! Good night.
Bye somebodyelse! Good night.

My regular expression search would be of the following form:

Hello .*! Have a good day.

How can I use the \1 keyword with sed (or any linux tool) to accomplish this.
Please help me with the actual command syntax.

Also, if you have a way to do this in notepad++, please let me know.

Thanks for any Help!

+1  A: 

Try:

sed 's/Hello \(.*\)! Have a good day./Bye \1! Good night./' MyInputFile > MyOutputFile
Al
Note that you can perform the replacement in place on the file using `sed -i 's/pat1/pat2/'` - best to check that your replacement is right first though (output to stdout, probably).
Jefromi
thanks all for the help!
mallik
A: 
echo "Hello somebody! Have a good day" | sed -e 's/Hello \(.*\) Have a good day/Bye \1 Good night./'

But I sense some misconception in how regexps work, since you're using "regexp1" in the source text. Regexps are patterns which describe strings, not the strings themselves.

JesperE