A Problem of Using vi/shell to Use Regular Expressions for Text Processing

  question, vim

The requirements are as follows:

Txt:
a1
a2
a3
a4
b1
b2
a5
b3
b4
b5

Txt is:
a1
a4
b1
b2
a5
b3
b5

That is to say, row n is removed, and n satisfies the condition that the initials of row n, row N-1, and row N+1 are the same (the first row and the last row are not removed).

Can I use the vi replacement command or shell to fulfill this requirement? Just give me a hint, thank you. (I have already implemented it in C++, and now I just want to know if it can be implemented in regular expressions.)

Regularity of vim

%s/\v((.).*\n)((\2.*\n)+)(\2.*$)/\1\5/g

Explain it in three paragraphs.

((.).*\n)
Match the first row, the outer grouping is used for reverse reference when replacing, and the inner grouping is used for later judgment.

((\2.*\n)+)
Match the line (1 or more lines) beginning with the first letter of the following previous line.

(\2.*$)
Match a line that begins with the first letter of the first line.

Finally \1\5 Replace all the rows matched above with the first row and the last row, that is, delete the middle row.

Note: The initial \v switch is to perl regular mode, so brackets and plus signs do not need to be escaped.

BTW is the most convenient text processing job to complete with perl scripts. The advantage of vi is visual debugging, but GG is used when large files are encountered.