Ubuntu – vim/gedit delete words between character

deletevim

I have a huge file with the entries like the one below:

<option value="201">Medical</option>
<option value="203">Finance</option>
<option value="205">Tax Return</option>
---------------------------
---------------------------

I am trying to delete 'options' entries in this file in one-shot so that file looks like the one below after editing:

Medical
Finance
Tax Return
----------
----------

I searched this in the google if I could do this with vim or gedit but didn't find any relevant to this(don't know searching with what keyword). Anybody has any idea, please?

Best Answer

You can use the following in vim to do what you want.

:%s/<[^>]*>\([^<]*\).*/\1/g

Here I use the s command, This is the complete syntax, you can check by typing :help :s

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

% for range means the whole file, {pattern} is an regular rexpression {string} means the string that has to be replaced. That can contain backreferences, i.e a part of the matched pattern enclosed within braces. [flags] are some extra options. g for global, i.e all the patterns in a line needs to be replaced. Other useful flag is c which asks for confirmation before changing. [count] must be the number of times, I guess.

So this can be read as, search for a lessthan< followed by any number or not a greaterthan> characters then a > then select any number of not a lessthan< characters into first group \1 then any number or any characters And replace this with the first group \1 globally.

Check this link to learn more about vim specific regular expression details http://www.softpanorama.org/Editors/Vimorama/vim_regular_expressions.shtml