亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

vim - A problem using regular expressions for text processing with vi/shell
黃舟
黃舟 2017-05-16 16:42:06
0
1
1003

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)

黃舟
黃舟

人生最曼妙的風(fēng)景,竟是內(nèi)心的淡定與從容!

reply all(1)
黃舟

vim’s regular expression

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

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template