Replace using vim – Replace a pattern […] by a string

regular expressionreplacevim

I have a file containing lines in below e.g. format –

[foo@host.com, bar@host.com], Payment processed - 23499, params = {'invoice':3243}

I only want the account numbers i.e. 23499. It's a number. Let's say it <account>.
Its not a constant.

For that, I am trying to –

  1. Remove params ...
  2. Replace […], from start of each line by whitespace

By ... I mean any string. I have tried –

# 1
:%s/params.*//g
# 2
:%s/\<[]\>//g
:%s/\<\[\]\>//g
:%s/\<[.*]\>//g
:%s/\<\[.*\]\>//g

All the things I have tried in # 2 have not worked. What am I doing wrong? How do I get <account>? Help me out.

Best Answer

You can use following sequence to only retain the account numbers (cudo's to J.D.Mohr)
note the space after the r in the command

:%norm $F,d$Bhv0r 

This assumes that there's only one , after the number you want to retain

Breakdown

:     -> Enter command mode
%norm -> Applies a normal command to the entire file
$     -> Jump to end of line
F,    -> Find preceding ,
d$    -> Delete until end of line
B     -> Jump back a word
hv0   -> Go left one character and select until beginning of line
r     -> replace selected text with <space>
Related Question