Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression in sed to replace C++ includes

Tags:

c++

include

sed

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!

like image 301
Skeen Avatar asked Jan 28 '26 12:01

Skeen


1 Answers

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.

like image 128
Jeff Paquette Avatar answered Jan 30 '26 01:01

Jeff Paquette