DevOps

How to Use sed Command to Delete a Line

Introduction

sed is a command-line stream editor used to filter and modify text in Linux, macOS, and other UNIX-based systems. This utility is non-interactive and works on a line-by-line basis.

The sed command is ideal for automating text manipulation tasks. Users can create scripts and utilize sed commands to delete, substitute, and insert text programmatically.

Learn how to use sed to delete lines of text that match specific patterns and conditions.

Sed parsing lines of text.

Prerequisites

  • Access to a command line/terminal window.
  • A text file (this guide provides an example input.txt file).

How to Use sed Command to Delete a Line

The basic sed command syntax includes the following elements:

sed OPTIONS [SCRIPT] [INPUTFILE]
  • OPTIONS. Options are used to modify the operations sed performs.
  • SCRIPT. The set of operations you instruct sed to complete.
  • INPUTFILE. The text file sed is going to alter.

Before proceeding, create a text file for testing sed delete commands.

1. Access the command line and use a text editor, such as nano, to create the input.txt file:

sudo nano input.txt

2. Copy the following content, which lists movies starring Bruce Willis, and save the file:

1. Blind Date (1987)
2. Die Hard (1988)
3. Look Who's Talking (1989)
4. Die Hard 2 (1990)
5. Look Who's Talking Too (1990)
6. Hudson Hawk (1991)
7. The Last Boy Scout (1991)
8. Loaded Weapon 1 (1993)*
9. Pulp Fiction (1994)
10. Die Hard With a Vengeance (1995)
11. Twelve Monkeys (1995)
12. The Fifth Element (1997)
13. The Sixth Sense (1999)
14. Ocean's Twelve (2004)*
15. Sin City (2005)
16. Die Hard 4.0 / Live Free or Die Hard (2007)
17. The Expendables (2010)*
18. RED (2010)
19. The Expendables 2 (2012)
20. A Good Day to Die Hard (2013)
21. RED 2 (2013)

Delete Single Specified Line

Use the d directive in sed to delete a single line in a text file:

sed [line_number]d' [file_path]

Replace [line_number] with the line number you want to delete and the [file_path] with the path to the text file.

Use the following command to delete line 5 from the input.txt file:

sed '5d' input.txt

By default, sed displays the changes made by the given command in its output and does not affect the source file.

Delete a single line using sed.

Use the -i option to save modifications and overwrite the original file:

sed -i '5d' input.txt

Note: The -i tag alters the source file. Ensure you have backups or version control in place when performing file manipulation operations.

Add > and a path to a new file at the end of the command to save the edits to a different or new file:

sed '5d' input.txt > [newfilepath]

Replace [newfilepath] with the path to the new .txt file.

Delete Last Line

The $ symbol represents the last line in sed syntax. Append d after $ to delete the last line of a file:

sed '$d' <file_path>

To delete the last line from the input.txt file:

sed '$d' input.txt

To delete the last line from the input.txt file and overwrite the file:

sed -i '$d' input.txt

To save the edits to a different (or new) file:

sed '$d' input.txt > [newfilepath]

Replace [newfilepath] with the actual path of the new .txt file.

Delete Multiple (Specified) Lines

Use a semicolon (;) to separate line numbers and delete multiple specified lines from a file:

sed 'xd;yd;zd;' [file_path]

To delete specified lines from the input.txt file, use:

sed '7d;12d;16d' input.txt
Delete multiple specified lines using sed.

Enter the following command to delete multiple specified lines from the input.txt file and overwrite the file:

sed -i '7d;12d;16d' input.txt

To save the edits to a different (or new) file:

sed '7d;12d;16d' input.txt > [newfilepath]

Replace [newfilepath] with the file path on your system.

Delete Range of Lines

Use a comma-separated range to specify and delete multiple lines:

sed '[start_line_number],[end_line_number]d' [file_path]

Replace [start_line_number] with the line number where you want to start deleting and [end_line_number] with the line number where you want to stop deleting. Both line numbers are inclusive, meaning that the start and end lines will be deleted. Replace [file_path] the path to your file.

Use the following command to delete lines 5 to 8 from the input.txt file:

sed '5,8d' input.txt
Delete a range of lines using sed.

To delete lines 5, 6, 7, and 8 and overwrite the input.txt file:

sed -i '5,8d' input.txt

To save the edits to a different (or new) file:

sed '5,8d' input.txt > [newfilepath]

Replace [newfilepath] with the actual path of the new .txt file.

Delete All Except a Specified Range

Enter the following command to delete all lines in a file except for a specified range:

sed '1, [start_line_number]d; [end_line_number],$d' [file_path]

Replace [start_line_number] with the line number where you want the specified range to start and [end_line_number] with the line number where you want the range to end. Replace [file_path] with the path to your file.

Use the following command to keep lines 4 to 6 in the input.txt file and delete all other lines:

sed '1,3d; 7,$d' input.txt

This command deletes lines 1 to 3 and 7 and above, effectively keeping only lines 4, 5, and 6.

Delete all lines except specified range in sed.

Use the following command to keep lines 4 to 6, delete all other lines, and overwrite the input.txt file:

sed -i '1,3d; 7,$d' input.txt

To save the edits to a different (or new) file:

sed '1,3d; 7,$d' input.txt > [newfilepath]

Replace [newfilepath] with the actual path of the new .txt file.

Delete Empty Line

Use the following command to delete empty lines (that contain no characters):

sed -i '/^$/d' [file_path]

The command uses a regular expression pattern ^$ to match empty lines. The ^ represents the start of a line, and $ represents the end of a line.

To delete empty lines from the input.txt file, use the following command:

sed '/^$/d' [file_path]

To delete all empty lines and overwrite the input.txt file:

sed -i '/^$/d' input.txt

To save the edits to a different (or new) file:

sed '/^$/d' input.txt > [newfilepath]

Replace [newfilepath] with the location of the new .txt file.

Delete if Line Matches Pattern

Enter the following command to delete lines that match a specific pattern:

sed '/[string]/d' [file_path]

Replace [string] with the specific string you want to search for. When sed identifies a line that contains the specified string, it deletes the line.

The command uses regular expressions to match the pattern within the forward slashes (/).

For example, use the following command to delete lines that contain the string Hard in the input.txt file type:

sed '/Hard/d' input.txt
Delete a line using sed if it matches a pattern.

To delete lines in the input.txt file if they contain the string Hard and overwrite the source file:

sed -i '/Hard/d' input.txt

sed performs simple string matching, so the matching is case-sensitive. If you want to perform a case-insensitive search, use the I flag:

sed -i '/hard/I d' input.txt

To save the edits to a different (or new) file:

sed '/hard/I d' input.txt > [newfilepath]

Replace [newfilepath] with the path of the new .txt file.

Delete Multiple Lines Based on Patterns

Use the following command to delete multiple lines based on patterns:

sed '/start_pattern/,/end_pattern/d' [file_path]

Replace start_pattern with the pattern in the line where you want to start deleting and end_pattern with the pattern in the line where you want to stop deleting. Patterns are enclosed within forward slashes / and can be regular expressions.

Use the following command to start deletion with input.txt lines that contain the pattern 1990 and end the deletion with lines that contain the pattern 1999:

sed '/1990/,/1990/d' input.txt
Delete multiple lines using sed patterns.

This command deletes lines starting with 1990 and ending with 1999 and overwrites the input.txt file:

sed -i '/1990/,/1999/d' input.txt

To save the edits to a different (or new) file:

sed '/1990/,/1999/d' input.txt > [newfilepath]

Replace [newfilepath] with the path of the new .txt file.

Delete Lines Not Matching Pattern

Use the following syntax to delete all lines that do not match a particular pattern:

sed '/pattern/!d' [file_path]

The exclamation mark (!) negates the pattern, deleting any line that does not contain it.

Enter the following command to delete everything except lines that contain the string 1991:

sed '/1991/!d' input.txt
Delete lines that do not match a pattern using sed.

To delete all lines except ones that contain 1991 and overwrite the input.txt file:

sed -i '/1991/!d' input.txt

To save the edits to a different (or new) file:

sed '/1991/!d' input.txt > [newfilepath]

Replace [newfilepath] with the file path on your system.

Delete Line if it Starts With Specified Character or Character Group

Use the following sed command to delete lines that start with a specified character or character group:

sed -i '/^[string]/d' [file_path]

Note: The -i option edits the file in place. If you want to test the command without modifying the file, you can omit the -i option and sed will output the modified content in the terminal.

Replace [string] with the desired character or character group you want to match. The (^) symbol represents the start of a line.

Enter the following command to delete lines that start with the character 1 from the input.txt file:

sed -i '/^1/d' file.txt
Delete lines that start with a character patterns using sed.

If you want to match a character group, such as deleting lines starting with either 1 or 2, you can use square brackets to define the group:

sed -i '/^[12]/d' input.txt
Delete lines that start with a character patterns using sed.

If you want to delete lines that start with a character group like [:digit:], use the following command:

sed '/^[[:digit:]]/d' input.txt

This command deletes all lines that start with a digit.

Delete Line if it Ends With Specified Character or Character Group

To delete lines that end with a specified character or character group, use the $ command:

sed -i '/[string]$/d' [file_path]

Replace [string] with the desired character or character group you want to match. The ($) symbol represents the end of a line.

Use the following command to delete lines that end with the character (*) from the input.txt file:

sed -i '/*$/d' input.txt
Delete lines that end with a character string using sed.

If you want to match a character group, such as deleting lines ending with either (*) or ), you can use square brackets to define the group:

sed -i '/^[*)]/d' input.txt

To delete lines that end with the [:blank:] character group, use the following command:

sed -i '/[[:blank:]]$/d' input.txt

This command deletes all lines that end with a space or a tab.

Delete Line That Matches the Condition and One After It

Use the following command to delete a line that matches a specific condition and the subsequent line:

sed '/[condition]/{N; /[condition]/d;}' [file_path]

Replace [condition] with the condition or pattern you want to match. sed searches for lines that match the pattern, and when a match is found, it appends the next line N to the pattern space. It proceeds to delete both the matching line and the following line.

Enter the following command to delete a line that contains the word The and the following line from the input.txt file:

sed -i '/The/{N; /The/d;}' input.txt
Using sed to delete lines based on conditionals.

The command assumes that there is always a line following the matching line. If there are cases where the matching line is the last line of the file or are multiple consecutive matching lines, additional modifications may be necessary.

Delete Line That Matches Condition and All Lines After It

Enter the following command to delete a line that matches a specific condition and all lines after it:

sed -i '/[condition]/,$d' [file_path]

Replace [condition] with the condition or pattern you want to match. This command searches for lines that match the pattern and deletes them, while the ($) flag instructs sed to delete all following lines.

For example, if you want to delete a line that contains the word Expendables and all lines after it from the input.txt file, use the following command:

sed -i '/Expendables/,$d' input.txt
Using sed to delete a line and all subsequent lines based on conditionals.

Character Groups

In <strong>sed</strong>, character groups are defined using square brackets []. They allow users to specify a set of characters that match a single character’s position. Use the table to insert entire character groups in sed commands instead of listing individual characters.

Character Groups Description
[:blank:] Blank characters include space and tab.
[:digit:] Digits 0-9
[:alpha:] Alphabetic characters (all letters).
[:alnum:] All digits and letters.
[:upper:] Uppercase letters.
[:lower:] Lowercase letters.
[:punct:] Punctuation characters in C and ASCII include: ! ” # $ % & ‘ ( ) * + , – . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~.
[:graph:] Graphical characters include all letters, digits, and punctuation characters.
[:space:] Space characters in the ‘C’ locale include tab, newline, form feed, vertical tab, space, and carriage return.
[:print:] Printable characters include all letters, digits, punctuation, and space.
[:cntrl:] In ASCII, Control Characters are assigned octal codes ranging from 000 to 037, including 177 (which represents the DEL character).
[:xdigit:] Hexadecimal digits include 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f.

The square brackets found in character group names are intrinsic to the symbolic names and should be used in addition to the brackets that define the bracket expression.

Conclusion

You now know how to use sed to delete specific lines based on patterns or line numbers.

This article focuses on deleting lines, but you can also use sed to replace and insert text, which increases your productivity and effectiveness when working on text files.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !