user2754673
user2754673

Reputation: 459

Deleting pattern from file using vim

I'm trying to delete pattern from a file.

Edge[15] = 415 
Edge[16] = 65 
Edge[17] = 835 
Edge[18] = 951 
Edge[19] = 999 
Edge[20] = 887 ...

How to delete everything before Numbers i.e till = , i.e i want just numbers and then wants to join using ,.

[415,65,835,...]

Upvotes: 0

Views: 164

Answers (2)

Alan Gómez
Alan Gómez

Reputation: 378

You can use two regex

:%s/.*\( \d\+\).*\n/\1,/g

:%s/^ \(.*\),.\{-}$/[\1]

Output:

[415, 65, 835, 951, 999, 887]

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172768

Okay, so you're asked for a one-liner instead of the multiple steps you've given. There surely are many other ways, here's one:

:%s/.*=\s*\(\d\+\)\n/\1, / | normal! $xr]^OI[

This does the removal of the stuff before = and the joining of lines both with :substitute (adapt the :% range to suit your needs), by capturing only the number. I then use normal mode commands to get rid of the trailing comma and add the square brackets. (Type ^O as Ctrl-V Ctrl-O; Ctrl-Q CTRL-O on Windows.) If you have the surround.vim plugin, this could be further simplified. One could also use a second :.substitute to achieve the same.

Actually, I probably would have used visual blockwise mode, a special J mapping, and normal mode to achieve this.

Upvotes: 2

Related Questions