The requirements are as follows:
Input file input.txt:
a1
a2
a3
a4
b1
b2
a5
b3
b4
b5
The required output file output.txt is:
a1
a4
b1
b2
a5
b3
b5
That is, remove the Nth line. The condition that N meets is: the first letters of the Nth line and the N-1 and N+1 lines are the same (the first and last lines are not removed).
Can I use vi's replacement command or shell to accomplish this requirement? I would also like to give you some tips. Thank you. (I have implemented it in C++, and now I just want to know if it can be implemented using regular expressions)
vim’s regular expression
Explain it in three paragraphs
((.).*n)
Match the first line, the outer grouping is used for back reference when replacing, and the inner grouping is used for subsequent judgment
((2.*n)+)
Matches the next line starting with the first letter of the previous line (1 or more lines)
(2.*$)
Matches a line starting with the first letter of the first line
The last 15 replace all the lines matched above with the first and last lines, that is, delete the middle lines
Note: The initial v is to switch to perl regular mode, so that brackets and plus signs do not need to be escaped
BTW It is most convenient to use perl scripts to complete this kind of text processing work. The advantage of vi is visual debugging, but it is GG when encountering large files