Extract an indented block from a file, starting from a given header line.
Usage: extract.awk HEADER file
e.g.
$ ./extract.awk a t
a() {
printf("lololol\n");
}
$ ./extract.awk b t
b() {
call_the_thing();
for(i = 1; i<a; i++) {
i--;
}
}
git clone https://gist.github.com/kousu/d6a945cfc6ffa9f93e82a630098acc90
mkdir -p ~/.local/bin
ln d6a945cfc6ffa9f93e82a630098acc90/extract-block ~/.local/bin
chmod +x ~/.local/bin/extract-block
and if you're not on a system where this is pre-configured, also do
echo 'export PATH=~/.local/bin:$PATH` >> ~/.profile # or equivalent
The idea is this would be useful in CI to detect when particular code blocks have changed.
To do this correctly requires a full AST parser. This is only an approximation. But most projects are using some kind of reasonable indentation rules, so it will work for them; and if you do use it to detect changes, it will detect when someone breaks the indentation rules too.