Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 756
  • Last Modified:

How to delete words using pattern in vi

Hi,

I am trying to find all words in my file that are 4 characters long and start with a B or b and delete those words. I am trying to break down the regular expression into parts and build onto it as I get things working. So far, I am just trying to delete words starting with a B using this vi command: g/^B/dw, but I get a 'Not an editor command: dw' error. Could someone point me in the right direction? thanks.
0
grnmachine
Asked:
grnmachine
2 Solutions
 
expert_tanmayCommented:
you have to use
:s/\<[Bb]\p\+//g
0
 
Hatrix76Commented:
What you are trying to do does not work with the range command.

The range command is for lines, if you do :g/^B/d then it deletes all lines beginning with B, if you do :g/Beg/d than all lines which contain Beg somewhere will be deleted.

With this command you can not delete words. Words only is best to delete with a search and replace command:

:s/[Bb].\{3\}//g

will do what you want, find words which starts with an B or b, and are exactly 4 characters long ( "." = any character and \{3\} means the entity before ( "." = any character) exactly 3 times, e.g. 6\{3\} matches 666), and replace them with nothing (s/<searchpattern>/<replacestring/g) the last g is a flag which means continue replacing after you find the first replacement on a line.

Standard regular expressions do not need the escaping for {} ... this is a bit difficult, perl regex, vi and sed, awk, grep have all different semantics regarding escaping ... so you'l have to read their respective manuals :-)

best


0
 
grnmachineAuthor Commented:
The command just seems to highlight the whole line that has four letter words starting with a B or b. I would like to have the command find four letter words and delete only those words starting with a B or b. I'm I entering the command incorrectly? Could you break down what each item in the expression means? I am just learning vi/regular expressions and its still a bit confusing. Thanks.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
expert_tanmayCommented:
Oh yes I missed out 4 characters long you have to use the following

:s/\<[Bb]\p\{3\}//g
0
 
grnmachineAuthor Commented:
@Hatrix76

Okay, let me try it out and I will get back to you. Thanks.
0
 
expert_tanmayCommented:
The alphabet s is for search/substitute
\< means starting of word
[Bb] means B or b
\p means printable characters
\{3\} means as it is followed by \p match 3 printable characters after B or b

// means substitute with nothing.
g means global in other words the entire file.
0
 
mohansahuCommented:
Hi,

For deleting a particular word in a file,Please use the following command.

:%s/\<mohan\>.\{0}//

Note:here Mohan is the word for delete in the file.

Cheers,
Mohan Sahu
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now