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

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)

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!