Match complete function blocks using awk

While working with killes on an occasion in Germany he needed the functionality to get all the given functions from a file, as if grepping complete functions, from the start till the closing }. He sent it to me, so I can save it for the lucky future, unserialized it for you to be able to read. It starts at the commend block above the code, and saves it until the closing bracket, and prints it.

It assumes that you have no leading space before the closing bracket, nor the function declaration, and also note that it matches theme_* functions.


BEGIN {
p=0;
}
{
if ($0 == "/**") { # start copying
p = 1; # copy flag 1
o = ""; # initial content
pp = 0; # encounter flag 0
}
if (p == 1) { # we are copying
o = o""$0"\n"; # append line to content
if ($0 ~ "^function theme_") {
pp = 1; # encounter flag 1
}
}
if ($0 ~ "^}") { #finish copying
p = 0; # copy flag 0
if (pp == 1) { # if we are at the end AND we encountered the function
print o; # print the block
o="";
}
}
}
END {}

Comments

Post new comment