开发者

Regular expression in sed to replace C++ includes

开发者 https://www.devze.com 2023-03-12 12:45 出处:网络
I\'m trying to play with sed, to change all; #include \"X\" to: #include <X> However I can\'t seem to find a wa开发者_JAVA技巧y to do this! - This is what I\'ve done so far:

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

#include "X"

to:

#include <X>

However I can't seem to find a wa开发者_JAVA技巧y 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!


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.


Try:

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


Try:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号