user2806363
user2806363

Reputation: 2593

How to find and remove part of word in vim?

I'm new into vim, I have hug text file as follow:

ZK792.6,ZK792.6(let-60),cel-miR-62(18),0.239
UTR3,IV:11688688-11688716,0.0670782
ZC449.3b,ZC449.3(ZC449.3),cel-miR-62(18),0.514
UTR3,X:5020692-5020720,0.355907

First, I would like to get delete all rows with even numbers (2,4,6...).

Second, I would like to remove (18) from entire file. as a example: cel-miR-62(18) would be cel-miR-62.

Third: How can I get delete all parentheses including it's inside? Would someone help me with this?

Upvotes: 0

Views: 224

Answers (2)

gregory
gregory

Reputation: 12895

  1. To remove every even line (or every other line):
:g/^/+d
  1. To remove every instance of (18):
:%s/(18)//g
  1. Remove all the parenthetical content:
:%s/(.\\{-})//g

Note: the pattern in third answer is a non-greedy match.

Upvotes: 1

mkorvas
mkorvas

Reputation: 603

For the first one:

:g/[02468]\>/d

where :g matches all lines by the regex between the slashes and runs d (delete line) on the matching lines. The regex is quite easy to read, the only interesting symbol there is perhaps the \>, which matches end of a word.

For the second question:

:%s/\V(18)//g

where % is the specification meaning "all lines of the file", s is the substitute command, \V sets the "very nomagic" mode of regexes (not sure what your default is, you might not need this) and the final g makes vim substitute all occurrences on each line (with an empty string, the one between slashes). Make sure that :set gdefault? prints nogdefault (the default setting of gdefault), otherwise, drop the final g from the substitute command.

Upvotes: 2

Related Questions