hzcodec
hzcodec

Reputation: 57

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?

Upvotes: 0

Views: 303

Answers (5)

Alan Gómez
Alan Gómez

Reputation: 378

Also works:

:%s/\%>15c.//g

Deletes from 15th column to right.

Upvotes: 0

jahroy
jahroy

Reputation: 22692

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)

Upvotes: 4

romainl
romainl

Reputation: 196456

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

qq
0
3f <-- space
D
q

then:

:%norm @q

Upvotes: 1

falsetru
falsetru

Reputation: 368894

If you want just remove last part:

:%s/[^ ]*$

If you want remove last part and its leading spaces:

:%s/ *[^ ]*$

Upvotes: 2

rid
rid

Reputation: 63442

You could move the cursor to the space before the first 0x00004341, press CtrlV 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

Upvotes: 6

Related Questions