简体   繁体   中英

Remove block of text in vim with regex

I have a file with:

mr l 0x19600000 0x00004341
mr l 0x19600004 0x00004820
mr l 0x19600008 0x00003130
mr l 0x1960000c 0x00003920

I would like to remove the last part of each row. So in my example above I want to remove

0x00004341
0x00004820
...

and so on.

The file consist of about 4000 rows so I guess a regex should be the way to do it.I've been trying this in vim without luck so far.

So the question is how to do this?

You could move the cursor to the space before the first 0x00004341 , press Ctrl V to enter visual mode, G to go do the end of the buffer, E to go to the end of the line, then d to delete.

Or, you could run:

%s/^\(.* \)[^ ]\+$/\1/g

Here's one simple way:

:%s/ [^ ]\+$//g

Here's some explanation:

  %      for the whole document
  s      substitute
  /      begin substitution pattern
         a space
  [^ ]   anything but a space
  \+     one or more of the previous pattern (must escape + with \)
  $      end of line
  /      end substitution pattern/begin replacement pattern
  /      end  replacement pattern (i.e. replace with empty string)
  g      perform multiple times per line (does nothing here)

If you want just remove last part:

:%s/[^ ]*$

If you want remove last part and its leading spaces:

:%s/ *[^ ]*$

Supposing all the lines look the same, you can do it with a macro:

qq
0
3f <-- space
D
q

then:

:%norm @q

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM