1

I have a pattern that I need to replace in my .hpp, .h, .cpp files in multiple directories.

I have read Find and replace a particular term in multiple files question for guidance. I am also using this tutorial but I am not able achieve what I intend to do. So here is my pattern.

throw some::lengthy::exception();

I want to replace it with this

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

How can I achieve this?

UPDATE:

Moreover, what if the some::lengthy::exception() part is variant such that it changes for every search result ? Something like

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

will be converted to

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

Community
  • 1
  • 1
Recker
  • 1,915
  • 25
  • 55

3 Answers3

2

You can use the sed expression:

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

And add it into a find command to check .h, .cpp and .hpp files (idea coming from List files with certain extensions with ls and 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 the usage of sed -i.bak in order to to edits in place but create a file.bak backup file.

Variable pattern

If your pattern varies, you can use:

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

This does the replacement in the lines starting with throw. It catches everything after throw up to ; and prints it back surrounded by 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();
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Ok.Thanks for this. But what if the `some::lengthy::exception()` part is variant? And changes for every search result ? Something like `throw [I change everytime]` will be converted to `throw CreateException([I change everytime]);` – Recker May 12 '15 at 09:48
  • @Recker for this I need you to update your question with a better set of sample input and desired output : ) – fedorqui May 12 '15 at 09:49
0

You could try the below sed command.

sed 's/\bthrow some::lengthy::exception();/throw CreateException(some::lengthy::exception());/g' *.cpp

Add inline-edit -i param to save the changes.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

You can use the following:

sed 's/\b(throw some::lengthy::exception());/throw CreateException(\1);/g'
karthik manchala
  • 13,492
  • 1
  • 31
  • 55
  • Except this won't work, since `sed(1)` needs BRE syntax while your answer uses Perl regexps. – lcd047 May 12 '15 at 09:36
  • 1
    Nope. Grouping `(...)` should be `\(...\)`, and `\b` doesn't exist in `sed(1)`. But `\1` is fine. – lcd047 May 12 '15 at 09:46
  • @lcd047 .. http://www.linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq6_009.html says `\b` can be used as word boundaries in sed.. no? – karthik manchala May 12 '15 at 09:50
  • Then they are wrong. `\b` has never been a word delimiter in `sed(1)`. It stands for the backspace character in BSD `sed(1)`, and it's undefined for most versions of GNU `sed(1)`. _shrug_ – lcd047 May 12 '15 at 09:56
  • Sorry, actually `\b` seems to work in recent versions of GNU `sed(1)`. It's probably still not a good idea to use it though. – lcd047 May 12 '15 at 10:02
  • @lcd047 .. well.. i dont use sed much.. but i saw answers (accepted) with `\b` as word boundary.. see http://stackoverflow.com/questions/1032023/sed-whole-word-search-and-replace – karthik manchala May 12 '15 at 10:03