This is a small post on how to delete a line from a file when it matched some criteria. We can use many tools and languages like sed, awk, grep, python, perl, ruby etc. In this post we specifically use SED which is by default installed in Linux.
SED is a powerful tool which is useful for text file modifications. GNU SED have one capability to edit files directly by using -i which is insert.
To delete a line from a file use below code
sed '/<search-pattern>/d' filename
// is used for searching a pattern and 'd' is used for deleting that line.
Example: Search for a line which contain tata and remove that line.
sed '/Managing/d' Jumpcloud
Output:
surendra@linuxnix:~$ cat Jumpcloud Creating users? Managing users? Deleting users? surendra@linuxnix:~$ sed '/Managing/d' Jumpcloud Creating users? Deleting users? surendra@linuxnix:~$
But this command will not update actual fill. In order to update use -i as mention earlier
sed -i '/<search-pattern>/d' filename
Example:
surendra@linuxnix:~$ cat Jumpcloud Creating users? Managing users? Deleting users? surendra@linuxnix:~$ sed -i '/Managing/d' Jumpcloud surendra@linuxnix:~$ cat Jumpcloud Creating users? Deleting users?
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