regex - find and replace strings in files in a particular directory -


i have pattern need replace in .hpp, .h, .cpp files in multiple directories.

i have read find , replace particular term in multiple files question guidance. using this tutorial not able achieve intend do. here pattern.

throw some::lengthy::exception(); 

i want replace this

throw createexception(some::lengthy::exception()); 

how can achieve this?

update:

moreover, if some::lengthy::exception() part variant such changes every search result ?

throw some::changing::text::exception();

will converted

throw createexception(some::changing::text::exception());

you can use sed expression:

sed 's/throw some::lengthy::exception();/throw createexception(some::lengthy::exception());/g' 

and add find command check .h, .cpp , .hpp files (idea coming list files extensions ls , grep):

find . -iregex '.*\.\(h\|cpp\|hpp\)' 

all together:

find . -iregex '.*\.\(h\|cpp\|hpp\)' -exec sed -i.bak 's/throw some::lengthy::exception();/throw createexception(some::lengthy::exception());/g' {} \; 

note usage of sed -i.bak in order to edits in place create file.bak backup file.

variable pattern

if pattern varies, can use:

sed -r '/^throw/s/throw (.*);$/throw createexception(\1);/' file 

this replacement in lines starting throw. catches after throw ; , prints surrounded createexception();`.

test

$ cat a.hpp  throw some::lengthy::exception(); throw you(); asdfasdf throw you(); $ sed -r '/^throw/s/throw (.*);$/throw createexception(\1);/' a.hpp  throw createexception(some::lengthy::exception()); throw createexception(you()); asdfasdf throw you(); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -