2014-09-18 106 views
0

我是grep的新手,在C和C++文件的重構過程中,我用包括#include <>替換系統的問題包含本地包含#include ""用#include「」替換#include「」

有沒有辦法做到這一點,使用grep與任何替代工具,說任何C或C++文件在給定的目錄和子目錄?

+0

沒有,'grep'是隻是一個[模式匹配搜索工具](https://en.wikipedia.org/wiki/Grep)。它不能取代內容。 – 2014-09-18 15:44:02

+3

應該可以用sed代替。 – Schoentoon 2014-09-18 15:45:39

+0

@NielsKeurentjes我知道,我的意思是使用grep來匹配包含模式,然後使用grep輸出進行替換。 – Manu343726 2014-09-18 15:46:07

回答

1

下面是一個解決方案使用sed

cat file.cpp | sed -re 's/#include\s*<([^>]+)>/#include "\1"/g' > fixed_file.cpp 
1

你可以在Perl中做到這一點:

perl -p -i.bak -e "s/<(.*)>/\"\1\"/g" abc.h 

或許更加安全:

perl -p -i.bak -e "s/#include <(.*)>/#include \"\1\"/g" abc.h