Skeen
Skeen

Reputation: 4732

Regular expression in sed to replace C++ includes

I'm trying to play with sed, to change all;

#include "X"

to:

#include <X>

However I can't seem to find a way to do this! - This is what I've done so far:

sed -i 's/#include ".*"/#include <.*>/g' filename

I think I'm in need of a variable to save the contains of ".*", i'm just unaware of how!

Upvotes: 3

Views: 1592

Answers (3)

Jeff Paquette
Jeff Paquette

Reputation: 7127

Yes, you do. Regexps use () to save the contents of a match and a \1 to retrieve it. If you use more than one set of (), then the 2nd match is in \2 , and so on.

sed -e 's/#include "\(.*\)"/#include <\1>/g' < filename

will do what you need.

Upvotes: 4

alternative
alternative

Reputation: 13042

Try:

sed -i 's/#include "\(.*\)"/#include <\1>/g' filename

Upvotes: 2

user2100815
user2100815

Reputation:

Try:

sed 's/#include "\(.*\)"/#include <\1>/' x.cpp

Upvotes: 2

Related Questions