Exclude some matches with sed

3

I have a series of files with entries(namespaces in source code) like

FW.WGS.Web.HHH.Controls, 
FW.WGS.Web.HHH.Email,  
FW.WGS.Web.HHH.Account, etc    

and some with entries

FW.WGS.Web.Controls, 
FW.WGS.Web.Email,      
FW.WGS.Web.Account, etc

Using sed (or some other linux/unix tool) I want to change all FW.WGS.Web.Controls/Email/Account/etc for FW.WGS.Web.HHH.Control/Email/Account/etc.

My current sed command matches 's/WGS.Web/WGS.Web.\HHH/g' is not suitable - I end up with entries like

FW.WGS.Web.HHH.HHH.Email

So I need to prevent sed from matching lines that already have "HHH" in them.

bryan

Posted 2011-02-11T19:58:48.223

Reputation: 7 848

Answers

9

In sed, you can use /pattern/!s/pattern/, e.g.

/HHH/!s/WGS\.Web/WGS\.Web\.HHH/g

Mikel

Posted 2011-02-11T19:58:48.223

Reputation: 7 890

1

perl -pe 's/WGS\.Web(?!\.HHH)/$&.HHH/g'

(where $& is a shortcut for the matched text)

See perlre.

user1686

Posted 2011-02-11T19:58:48.223

Reputation: 283 655