Alain1405 Alain1405 - 1 year ago 77
Bash Question

How to flatten #include paths in multiple file

I'm working on a C++ project with multiple libraries in subfolders. The cloud IDE I'm using requires all files to be in the same folder to use cloud flashing features.
I now have 2 scripts. One that flattens the directory structure and one that flattens the includes to match the new layout.
So far I've been adding the following to the script I use to replace absolute path of the includes:

find $(_CLOUD_SRC_FOLDER) -type f \( -iname \*.cpp -o -iname \*.hpp \) -maxdepth 3 -exec sed -i.bak 's/\#include "..\/lib\//#include "/g' {}

The above line only works for
#include "../lib/filename.hpp"
. How can I reformat this to match
#include "any/path/filename.hpp"
and replace with #include "filename.hpp"?

Answer Source

Try this:

find $(_CLOUD_SRC_FOLDER) -type f \( -iname \*.cpp -o -iname \*.h  -o -iname \*.hpp \) -maxdepth 3 -exec sed -i.bak 's/\(#include "\)[^"]*\/\([^"]*"\)/\1\2/' {} +

#include string and filename are captured from path and output using backreference.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download