The repository I am working on has multiple source files in a non-flat directory structure, e.g.
project
│ main.cpp
│
└───moduleA
│ A.cpp
All these source files have code something like
#include "A.h"
WINDOWS_DISABLE_WARNING
#include "externalA.h"
#include "externalB.h"
WINDOWS_ENABLE_WARNING
// more code
How can I use sed
to replace all quotation marks include with angle brackets include between WINDOWS_DISABLE_WARNING
macro and WINDOWS_ENABLE_WARNING
macro for all files like below recursively?
#include "A.h"
WINDOWS_DISABLE_WARNING
#include <externalA.h>
#include <externalB.h>
WINDOWS_ENABLE_WARNING
// more code
I know how to replace simple strings recursively like find ./ -type f -exec sed -i 's/oldtext/newtext/' {} \;
, but I am struggling to figure out how to replace quotation marks with angle brackets between two patterns.
The reason I am doing is this is because Visual Studio recently introduced /external:anglebrackets
, if this flag is enabled, any headers included with angle brackets are treated as external, and the warnings coming from it can be turned off via /external:W0
. That means no more ugly macros like WINDOWS_DISABLE_WARNING
which is defined as __pragma(warning(push,0))
if on Windows, otherwise empty, via CMake's target_compile_definitions
and similarly WINDOWS_ENABLE_WARNING
which is defined as __pragma(warning(pop))
if on Windows, otherwise empty, via CMake's target_compile_definitions
. Since using sed
to remove the macros recursively is pretty straightforward, eventually the code can become clean like the below when working with loosely written third-party libraries with the new /external
flags enabled.
#include "A.h"
#include <externalA.h>
#include <externalB.h>
// more code
CodePudding user response:
Read about addressing lines in sed
find ... -exec sed -i '/WINDOWS_DISABLE_WARNING/,/WINDOWS_ENABLE_WARNING/ { s/"/</; s/"/>/ }' '{}'
Make sure to back your files up (sed -i.bak
) (or omit the -i
option) before experimenting with -i