Problem:
I have multiple large files that contain the following code block(s) split across all of them:
type Query {
...
}
extend type Query {
...
}
extend type Query {
...
}
I wish to parse all files and extract this Query
block and have it merged into a single file.
The code between the Query
blocks and number of lines differs from one file to the next. And there could be different blocks before/after the Query
blocks - all demarcated with {}
.
I understand simple regexes will not work since they could skip over a closing brace and match the entire file instead.
Other than writing a program to read the file line-by-line are there other UNIX-y ways of solving this problem? My guess is what I want to do is "structural match" and that implies something like a CFG (context-free-grammar) parsing and may signal to something like YACC. Although I'm not sure if there's an easier solution and how to go about it.