Solved

How to delete words using pattern in vi

Posted on 2010-11-19
7
749 Views
Last Modified: 2012-06-27
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
Comment
Question by:grnmachine
7 Comments
 
LVL 9

Expert Comment

by:expert_tanmay
ID: 34176748
you have to use
:s/\<[Bb]\p\+//g
0
 
LVL 7

Accepted Solution

by:
Hatrix76 earned 125 total points
ID: 34177036
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
 

Author Comment

by:grnmachine
ID: 34177082
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 9

Assisted Solution

by:expert_tanmay
expert_tanmay earned 125 total points
ID: 34177084
Oh yes I missed out 4 characters long you have to use the following

:s/\<[Bb]\p\{3\}//g
0
 

Author Comment

by:grnmachine
ID: 34177087
@Hatrix76

Okay, let me try it out and I will get back to you. Thanks.
0
 
LVL 9

Expert Comment

by:expert_tanmay
ID: 34177148
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
 
LVL 6

Expert Comment

by:mohansahu
ID: 34185901
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question