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)
- 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
- wget download a file to a directory in Linux/Unix - June 4, 2018
- GIT: How to compare two GIT branches? - June 3, 2018
- Online training on Linux Bash shell scripting - February 8, 2018