search

Home  >  Q&A  >  body text

vim multi-line matching delete to the beginning of the line

Problem description
Needs to process a larger file and delete the content before the matching string in each line. The file content format is as follows:


aaa dd kk segmentfault=1098-0987-3jlc ok=okj hello=0cvqa
12jk od=kk segmentfault=1jj8-lk87-oplc suiji=che shachu=keng
pppp=dd bbo;=9as segmentfault=0876-oj87-3pac zifu=ka neirong=xia

It is expected that the content before the first occurrence of segmentfault in each line can be deleted to the beginning of the line. The expected processing results are as follows


segmentfault=1098-0987-3jlc ok=okj hello=0cvqa
segmentfault=1jj8-lk87-oplc suiji=che shachu=keng
segmentfault=0876-oj87-3pac zifu=ka neirong=xia

Current solution
Use macros to execute

qa    ---开启宏录制
/resourceId ---随意操作
d0               ---随意操作,删除至行首
j                  ---随意操作,跳转到下一行
q     ---退出宏
1000000@a ---执行1000000次

Disadvantages: low efficiency, extremely time-consuming

Expected solution
It is expected that after direct column editing (ctrl + v), the operation of deleting the matching position of each row to the beginning of the row will be performed.
How to achieve this?

ringa_leeringa_lee2778 days ago665

reply all(3)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 16:36:34

    Try regular replacement and column editing. Your keywords are in different positions and cannot be edited directly

    :%s/^.*segmentfault/segmentfault/g

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 16:36:34

    @ixx The idea is right, regular expressions
    but there is a simpler solution

    :%s/^.*\zesegmentfault//g

    ze Mark matching mode ended

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 16:36:34

    I guess the author of the question wants to use vim's column mode (Ctrl-v) and then select multiple lines to match the part before the word segment.
    But it doesn’t work, because it can’t be done
    1) In column mode, matching is better done at the beginning or end of the line, forming irregular matching blocks; other positions cannot.
    2) The s from the beginning of the line to the segment in the first line is 3 W, while the s from the 2nd and 3rd lines to the segment is 2 W. This is not easy to do at once. (For the same reason, replacing W with w won’t work either)

    Column mode is more suitable for processing if the text is arranged neatly or can be easily positioned .

    ==========
    Another answer said that using regular expressions to match and replace is very useful.

    reply
    0
  • Cancelreply