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?

 

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.