This is bit tricky question. With SED it’s bit easy to do. In vi editor too we can search for a word and delete it with some trick.
Delete matched search term from a file
Step1: Go to command mode and search mode
Step2: Now search for your term and replace it with nothing
This will help you to delete all the occurrences of your search term. Let me explain above syntax
:%s is for searching for entire file, if you want to search in a particular line we can just use :s.
/serchterm// will replace ‘searchterm’ with nothing, which means it will be removed from that line
g for global removal, what it mean is the search and replace operation is applicable for all the occurrences of search-term in a given line.
Delete matched search term line from a file
Some times it’s require to delete entire line of your searched term. We can use below code once you go to command mode
Deleting reverse or inverse of search term lines from a file
We can even delete all the lines which do not contain our search term with below code.
Hope this helps, check our other vi editor posts as well.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix- June 29, 2020
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018