Solved

How to delete words using pattern in vi

Posted on 2010-11-19
7
743 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
Comment Utility
you have to use
:s/\<[Bb]\p\+//g
0
 
LVL 7

Accepted Solution

by:
Hatrix76 earned 125 total points
Comment Utility
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
Comment Utility
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 9

Assisted Solution

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

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

Author Comment

by:grnmachine
Comment Utility
@Hatrix76

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

Expert Comment

by:expert_tanmay
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 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

14 Experts available now in Live!

Get 1:1 Help Now