Link to home
Start Free TrialLog in
Avatar of grnmachine
grnmachineFlag for Canada

asked on

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.
Avatar of expert_tanmay
expert_tanmay
Flag of Singapore image

you have to use
:s/\<[Bb]\p\+//g
ASKER CERTIFIED SOLUTION
Avatar of Hatrix76
Hatrix76
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grnmachine

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Hatrix76

Okay, let me try it out and I will get back to you. Thanks.
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.
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