sed Remove Line Containing String

sed remove line containing string

Using sed Command

Read the provided text file and remove lines containing string using sed command. We will not modify the input file (file.txt) but display the output on the Bash console. We can use -i option in case we want to modify input file

Let’s break down command to understand it better.

  • The sed is a stream editor command; we used it to manipulate the specified text files.
  • The /number/ pattern matched all the lines containing the pattern.
  • The /d is a sed command, instructing it to delete the matched lines.
  • The file.txt was the input file name

Here /number/ is pattern here and d operator is used for deleting the line which contains the pattern.

You can replace number with particular text for which you want to remove line containing that particular text.

We can use the -i option with the sed command to directly modify the input file (file.txt).

We can use -i.bak with the sed command to create a backup of the input file (file.txt) first and then modify it directly. The backup file will be named as inputFileName.Extension.bak

If we don’t want to modify the input file (file.txt) then we can store the updated content in a separate output file (output.txt) using the redirection operator (>).**

The sed is a command-line utility for stream editing. In all the above examples, we use the sed command to read the input file and process it line by line. Also, the /number/ is the pattern to be matched while the d is used to delete the matched pattern.

That’s all about sed remove line containing string.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *