Solved

How to delete words using pattern in vi

Posted on 2010-11-19
7
752 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

710 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