Home » Tutorials » Linux Tutorials » Sed Command to Delete Lines in File in Linux with Examples

Sed Command to Delete Lines in File in Linux with Examples

Sed Command to Delete Lines: Sed command can be used to delete or remove specific lines which match a given pattern or are in a particular position in a file. Here we will see how to delete lines using the sed command with various examples.

The following file contains sample data which is used as an input file in all the examples:

cat file

Output:

linux
unix
fedora
debian
ubuntu

Sed Command to Delete Lines – Based on Position in File

In the following examples, the sed command removes the lines in the file that are in a particular position in a file.

1. Delete first-line or header line

The d option in the sed command is used to delete a line. The syntax for deleting a line is:

sed 'Nd' file

Here N indicates Nth line in a file. In the following example, the sed command removes the first line in a file.

sed '1d' file

Output:

unix
fedora
debian
ubuntu

2. Delete last line or footer line or trailer line

The following sed command is used to remove the footer line in a file. The $ indicates the last line of a file.

sed '$d' file

Output:

linux
unix
fedora
debian

3. Delete a particular line

This is similar to the first example. The below sed command removes the second line in a file.

sed '2d' file

Output:

linux
fedora
debian
ubuntu

4. Delete a range of lines

The sed command can be used to delete a range of lines. The syntax is shown below:

sed 'm,nd' file

Here m and n are min and max line numbers. The sed command removes the lines from m to n in the file. The following sed command deletes the lines ranging from 2 to 4:

sed '2,4d' file

Output:

linux
ubuntu

5. Delete lines other than the first line or header line

Use the negation (!) operator with the d option in the sed command. The following sed command removes all the lines except the header line.

sed '1!d' file

Output:

linux

6. Delete lines other than the last line or footer line

sed '$!d' file

Output:

ubuntu

7. Delete lines other than the specified range

sed '2,4!d' file

Output:

unix
fedora
debian

Here the sed command removes lines other than 2nd, 3rd, and 4th.

Related:  How to Install Lazy Script in Kali Linux?

8. Delete the first and the last line

You can specify the list of lines you want to remove in the sed command with a semicolon as a delimiter.

sed '1d;$d' file

Output:

unix
fedora
debian

9. Delete empty lines or blank lines

sed '/^$/d' file

The ^$ indicates sed command to delete empty lines. However, this sed does not remove the lines that contain spaces.

Sed Command to Delete Lines – Based on Pattern Match

In the following examples, the sed command deletes the lines in the file which match the given pattern.

10. Delete lines that begin with the specified character

sed '/^u/d' file

Output:

linux
fedora
debian

^ is to specify the starting of the line. Above sed command removes all the lines that start with the character u.

11. Delete lines that end with the specified character

sed '/x$/d' file

Output:

fedora
debian
ubuntu

$ is to indicate the end of the line. The above command deletes all the lines that end with the character x.

Related:  The Linux File System Structure Explained

12. Delete lines that are in upper case or capital letters

sed '/^[A-Z]*$/d' file

13. Delete lines that contain a pattern

sed '/debian/d' file

Output:

linux
unix
fedora
ubuntu

14. Delete lines starting from a pattern till the last line

sed '/fedora/,$d' file

Output:

linux
unix

Here the sed command removes the line that matches the pattern fedora and also deletes all the lines to the end of the file which appear next to this matching line.

15. Delete the last line only if it contains the pattern

sed '${/ubuntu/d;}' file

Output:

linux
unix
fedora
debian

Here $ indicates the last line. If you want to delete the Nth line only if it contains a pattern, then in place of $ place the line number.
Note: In all the above examples, the sed command prints the contents of the file on the UNIX or Linux terminal by removing the lines. However, the sed command does not remove the lines from the source file. To remove the lines from the source file itself, use the -i option with the sed command.

sed -i '1d' file

If you don’t wish to delete the lines from the original source file you can redirect the output of the sed command to another file.

sed '1d' file > newfile

Leave a Comment