In this article, we will guide you on “sed command to delete lines” with the help of the sed command, you can remove some lines according to your need. In this blog post, we will guide you on how to delete lines using the sed command. We have written a complete guide for you to make sure you read these sed examples carefully.
Sed Command to Delete Lines
If you want to fix your problem then the sed command plays the most important role in file manipulation.
Behind below a sample, data is for you to understand.
# cat sed-demo.txt centos red hat fedora arch Linux rhel Debian ubuntu OpenSuse UNIX operating system Linux operating system
Delete lines that contain a pattern
> sed '/debian/d' file linux unix fedora ubuntu
Delete blank lines or empty lines
Keep in mind ^$ express sed command not removing any lines that have to contain spaces. ^$ is for delete empty lines.
> sed '/^$/d' file
Delete a particular line
> sed '2d' file linux fedora debian ubuntu
Keep in mind this sed command deletes the second line.
Delete first-line or header line
Keep in mind “d” sed command is used to delete a line.
> sed 'Nd' file
And “N” express “Nth” line in a file. So keep in mind sed command removes the first line.
> sed '1d' file unix fedora debian ubuntu
Delete the first and the last line
So which line you want to remove in the sed command.
> sed '1d;$d' file unix fedora debian
If it contains the pattern “Delete last line only”
$ symbol express the last line. So if you are thinking to delete “AIX” then you need to replace “$” symbol with the line number.
$ sed '${/AIX/d;}' file Cygwin Unix Linux Solaris
Keep in mind this command is used to remove the footer line and $ symbol to express the last line of a file.
> sed '$d' file linux unix fedora debian
Delete all lines That with this character
> sed '/x$/d' file fedora debian ubuntu
Delete a range of lines
> sed 'm,nd' file
> sed '2,4d' file linux ubuntu
Delete lines first-line or header line
> > sed '1!d' file linux
Delete lines that start with these characters
> sed '/^u/d' file linux fedora debian
Delete lines that are capital letter or uppercase
> sed '/^[A-Z]*$/d' file
Delete lines containing the pattern ‘Linux’ OR ‘Unix’
$ sed '/Unix\|Linux/d' file Cygwin Solaris AIX
Delete a range of lines
So below sed command is used for remote 2nd line to 4th line.
$ sed '2,4d' file Cygwin AIX
Ending with either x or X Delete lines
$ sed '/[xX]$/d' file Cygwin Solaris
Delete a particular line
$ sed '3d' file Cygwin Unix Solaris AIX
Delete first line & last line of a file
$ sed '1d;$d' file Unix Linux Solaris
Delete Pattern Matching Line + Next Line
# sed '/System/{N;d;}' sed-demo.txt After deletion: 3 RHEL 4 Red Hat 5 Fedora 6 Arch Linux 7 CentOS 8 Debian 9 Ubuntu 10 openSUSE
Delete the last line from a File
# sed '$d' sed-demo.txt After deletion: 1 Linux Operating System 2 Unix Operating System 3 RHEL 4 Red Hat 5 Fedora 6 Arch Linux 7 CentOS 8 Debian 9 Ubuntu
Delete Empty or Blank Lines
# sed '/^$/d' sed-demo.txt After deletion: 1 Linux Operating System 2 Unix Operating System 3 RHEL 4 Red Hat 5 Fedora 6 Arch Linux 7 CentOS 8 Debian 9 Ubuntu 10 openSUSE
Delete only next line containing
$ sed '/Unix/{N;s/\n.*//;}' file Cygwin Unix Solaris AIX
Conclusion
I hope this article helpful for you to fix your problem. we collect most using sed line command list for you i hope you understand every single point or if you have some question in your mind please leave a comment i will reply to you soon and fix your issue and also share this post with others in your community.
Also, Read: