“Sed” stands for “Stream Editor”, and is a Linux tool designed for modifying input streams of text on a line-by-line basis. When compared to other text manipulation tools like awk and grep, sed is used for quick editing functions. Awk, on the other hand, is a full-fledged programming language with control structures and string manipulation. Sed, awk, and grep are all different tools focusing on text. Even though sed was first developed in the 1970s, it’s still incredibly useful even today.
One of the biggest benefits of sed is that it operates on a line-by-line basis, meaning that even arbitrarily large text streams can be processed efficiently, compared to other editors which attempt to load the entire file into memory before beginning processing.
In this article, I’ll show you how to delete lines using sed. The focus will be on deleting specific line numbers, ranges of lines, and multiple standalone lines along with exclusions.
Sed Syntax for Deleting Lines
The syntax of sed looks like this:
sed [OPTIONS] 'COMMAND' file
The “command” is a string that takes the format:
[address]X[options]
The “address” is either the line number of the lines on which you want sed to operate, or a pattern, which we’ll discuss below. “X” refers to what you want to do with the lines once they’ve been matched. In our case, since we want to delete lines, we use the letter “d”. Keep in mind that in sed, the numbering starts from 1 and not zero. So the first line in any document is always “1”.
Putting all this together, here’s a basic syntax for how to delete a single line in sed:
sed 'Nd' file.txt
Here, “N” refers to the line number, and “d”, as mentioned above, is “delete”. Here’s the output of the above command on a text file “example.txt”, in which I’ve numbered the lines and am deleting the 3rd line:
As you can see, the 3rd line is gone.
Sed Doesn’t Save Changes to its Output
An important point to remember is that sed doesn’t automatically save the changes to the text stream back to the file. So in the above example, the file “example.txt” is still unchanged! Sed merely sends its output to the standard output – which is usually the terminal screen. This is called a “non-destructive” operation and is one of the key aspects of sed from a safety perspective.
If you want to save the changes you make to the input file, you must use the “-i” flag like this:
sed -i '3d' example.com
The above command will not only remove the 3rd line from the output but save the changes back to “example.txt”. As you can imagine, this is a dangerous option and should only be exercised carefully.
Two Ways of Taking Backups When Deleting Lines with sed
To avoid the dangers of permanently changing files with sed, there are two ways you can take a backup. The first is by redirecting the output to another file like this:
sed '3d' example.txt > newfile.txt
The above example will save the changes to “newfile.txt” and won’t touch “example.txt”.
The second way of saving a backup is through an extension of the sed “-i” option. For example, the following command:
sed -i.bak '3d' example.txt
This will save the changes to example.txt, but will also save a backup copy of the file called “example.txt.bak”. The syntax for the option is:
-i[SUFFIX]
Here, [suffix] is the extension you want to use for the backup file. It can be anything you want.
The above two techniques help you remain safe while using sed, and allow you to experiment with various removal techniques without any danger of permanent damage.
Deleting Multiple Lines and Ranges with sed
In the above simple example, I showed you how to delete a single line with sed. But you can do a lot more than just remove single lines. In this section, I’ll show you how to delete multiple lines with a single command, delete line ranges, delete lines that match a pattern, and delete lines that don’t match a pattern.
Deleting Multiple Lines at the Same Time
Let’s say you want to remove the first and third lines from a text file. Using sed, you can do this with the following syntax:
sed 'Nd;Md' example.txt
Where “N” and “M” are the line numbers you want to remove. You specify each line you want to remove and separate them with a semicolon (;). You can add as many line numbers as you want.
Deleting Line Ranges
If you want to delete a chunk of text between two lines, then sed allows you to specify the start and end lines using the following syntax:
sed 'N,Md' file.txt
So when you want to delete a range, get the starting and ending line numbers, plug them into the above command, and you’re set!
Combining Range Deletions and Single Line Deletions
Sed even allows you to combine the two. You can delete a range of lines, as well as a bunch of single lines using the same command. For example, the following command:
sed 'N,Md;Od' file.txt
This will delete the range of lines from N to M as mentioned above, but it will also delete the line number represented by “O”. As you can see, sed is very flexible and you can accomplish several tasks using the same command if you know the syntax.
Deleting the Last Line
You can also delete the last line of a file by typing:
sed '$d' example.txt
Deleting Lines that Match a Pattern
Since sed parses input line by line, you can specify a pattern that you want to identify, and then delete those lines. For example, to find a basic pattern, you can use:
sed '/apple/d' example.txt
This will find and delete all the lines in example.txt with the text “apple” in them.
Case Insensitive Matching
By default, sed takes the case of the pattern into account. So “apple” will also match “Apple”. If you want to remove all lines that match a pattern regardless of case, then you can instruct sed to ignore the case by using the following command:
sed '/apple/Id' example.txt
Note the extra “I” before the delete command.
Matching Patterns at the Beginning of Lines
Let’s say you have a code file that you want to streamline for production and remove all comments. Different languages have different syntaxes for comments. Python and Linux files, for example, use the hash (#) character. So if you want to remove all lines in a file that start with “#”, use the following:
sed '/^#/d' example.txt
The symbol denoting the start of the string is the caret (^), followed by “#”. This single command will remove all comments from a Linux script or a Python code file. As you can probably guess, this syntax is part of the regular expression pattern, which means you can use RegEx as well for more sophisticated matching.
Conclusion
There’s a lot more to sed than what I’ve explained above. You can use regular expressions, and even perform complex operations like using RegEx to surround specific strings with parentheses, and more. Don’t forget to redirect the output to another file, or to create a backup by using the “-i[EXTENSION]” parameter for in-place editing. Sed is a powerful tool, but it can cause havoc if you use it carelessly.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply