Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

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

share|improve this answer
    
Awesome!!!. If possible could you explain me, because it doesn't requre me to post again if anything similar I have to do. Anyhow, Thank a lot! –  user3215 Mar 25 '11 at 13:47
    
@user3215 Added explanation. –  balki Mar 25 '11 at 18:43

Install surround.vim, which provides a normal-mode mapping dst that should do the job.

In order to apply this to multiple lines, select them in the visual mode and invoke

:'<,'>normal dst

This will apply the normal-mode command dst to the selected area. If you want to apply it to the entire file (not only the visual selection), use the following command

:%normal dst
share|improve this answer
    
Which command would help in this specific question? –  jrg Mar 25 '11 at 8:32
1  
I've changed the formatting to make it more obvious that I mean dst. –  Adam Byrtek Mar 25 '11 at 8:44
    
Nicely done. :) –  jrg Mar 25 '11 at 8:49
    
I have just checked :%normal dst with normal vim, it properly arranged <option> entries line by line and I actually wanted to mention this but forgot. Nice!!!. I have no much knowledge of visual mode(don't know using properly) and I didn't not understand how do I delete the <options...> and </option> –  user3215 Mar 25 '11 at 9:26
    
Thanks a lot!. Your answer saved more time. –  user3215 Mar 25 '11 at 13:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.