grep -v -B1 does not work because it will skip these lines but turn them on later (due to -B1 . To check this, try the command:
**document 1** -> 1 **page 1 of 2** -> 2 **document 1** **page 2 of 2** **page 3 of 2**
You will notice that the page 2 line will be skipped because this line will not be matched, and the next one, as usual, will not match.
There is a simple awk solution:
awk '!/page.*of.*/ { if (m) print buf; buf=$0; m=1} /page.*of.*/ {m=0}' 1.txt
The awk command says the following:
If the current line has this "page ...", then it will signal that you did not find a valid line. If you do not find this line, you print the previous line (stored in buf) and reset in the buffer of the current line (therefore, making it lag by 1)
source share