I am trying to copy to a text file any 'if' blocks from a C++ code base where the expression matches a certain pattern. Is this possible using some combination of grep/awk/sed etc?
Example
If I have files that contain code like:
//File1.cpp
if(/*matching-expression-1*/)
{
//Code
}
//File2.cpp
if(/*non-matching-expression*/)
{
if(/*matching-expression-2*/)
{
//Code
}
}
//File3.cpp
if((/*matching-expression-3*/)
{
if(/*non-matching-expression*/)
{
//Code
}
}
I would like to get a result like:
//OutputFile.txt
File1.cpp:
if(/*matching-expression-1*/)
{
//Code
}
File2.cpp:
if(/*matching-expression-2*/)
{
//Code
}
File3.cpp:
if((/*matching-expression-3*/)
{
if(/*non-matching-expression*/)
{
//Code
}
}
I'm okay with the //Code
block containing other matching/unmatching if
blocks, even if that leads to repeat entries, and it's not necessary for the tab indent to be preserved.
I have no trouble using grep to match the expressions I want, but that only gives me the lines containing the start of the 'if' block (which is a good start!) but I am unsure how to proceed.
Any help at all would be appreciated!