Re: Replace Text in the middle of string with wild character
Tim Chase <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
On 08/08/12 06:10, KRiTs wrote: > I have large file which has following lines > > <SHOUT DEST="ECS" MESSAGE="JOBNAME HAS ENDED NOTOK" URGENCY="R" WHEN="NOTOK"/> > > Now I want append a word after MESSAGE=" with a condition that the line ends with WHEN="NOTOK"/> > > as there are other lines ending with WHEN="OK"/> which should not be touched. > > Challenge is the actual text within MESSAGE "" would vary > > I tried \1 \2 option also but to no avail. You were on the right track with the \1 notation, but you don't mention what you tried. I used the following and it seemed to do what you describe (though I don't know whether you want to add text "before" or "after", so you can modify accordingly as you see fit): sed '/WHEN="NOTOK"/s/MESSAGE="\([^"]*\)"/MESSAGE="before \1 after"/' large_file.txt which roughly translates to "on lines that match /WHEN="NOTOK"/ capture the MESSAGE="..." contents and then replace them with your modifications, inserting the original text back in as \1" -tkc