Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a certain number of characters after a string in vim?

Tags:

vim

I have a large file that I need to edit a certain number of characters out of after a certain pattern. This is an example of part of my file:

@IB4UYMV03HCKRV
100 100 100 100 100 100 100 100 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 39 39 39 40 40 40 40

@IB4UYMV03GZDSU
100 100 100 100 100 100 100 100 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 39 39 39 40 40 40 40 40 40 40 40 40 40 40 39 39 39 40 40 40 40 40 40 40 40 40 "

After the 100 100 100 100 100 100 100 100 I would like to delete the next twelve 40 which is 36 characters with spaces. I tried using a find and replace but the characters are not always 40, they could be any two figure number.

Is there a way to delete 36 characters after a pattern?

like image 766
user2776110 Avatar asked Sep 13 '13 10:09

user2776110


1 Answers

If you put the cursor over the first 4 in your string of 12 40s, you can do 36x in normal mode which will delete a single character 36 times.

Or, if you've got a regular pattern, you can use the following substitution:

:%s/^\v(100 ){8}\zs(\d\d ){12}/

Broken down:

Ex Command:
%              All lines
s              Substitute

Pattern:
^              Anchor to start of line
\v             Very magic mode, to avoid excessive slashes
(100 ){8}      8 instances of the string "100 "
\zs            Start selection
(\d\d ){12}    12 instances of the string "\d\d " (i.e., \d is any digit)

Replacement:
               Nothing (i.e., remove it)

The "selection" is the part that is ultimately replaced. Note that there is no matched \ze (end selection), because we don't care how the pattern ends.

like image 91
Xophmeister Avatar answered Sep 25 '22 11:09

Xophmeister