Solved

How to delete words using pattern in vi

Posted on 2010-11-19
7
747 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now