- Linux sed delete string
- Sed Command to Delete a Line
- Sed on Linux
- Deleting line using sed
- Delete single line
- Delete a range of line
- Delete multiple lines
- Delete all lines except specified range
- Delete empty lines
- Delete lines based on pattern
- Delete lines starting with a specific character
- Delete lines ending with specific character
- Deleting lines that match the pattern and the next line
- Deleting line from the pattern match to the end
- Final thought
- About the author
- Sidratul Muntaha
Linux sed delete string
In this article of sed tutorial series, we are going to see how to delete or remove a particular line or a particular pattern from a file using the sed command.
Let us consider a file with the sample contents as below:
$ cat file Cygwin Unix Linux Solaris AIX
$ sed '1d' file Unix Linux Solaris AIX
The above command will show the file content by deleting the first line. However, the source file remains unchanged. To update the original file itself with this deletion or to make the changes permanently in the source file, use the -i option. The same is applicable for all the other examples.
Note: -i option in sed is available only if it is GNU sed. If not GNU, re-direct the sed output to a file, and rename the output file to the original file.
2. Delete a particular line, 3rd line in this case:
$ sed '3d' file Cygwin Unix Solaris AIX
$ sed '$d' file Cygwin Unix Linux Solaris
4. Delete a range of lines, from 2nd line till 4th line:
The range is specified using the comma operator.
5. Delete lines other than the specified range, line other than 2nd till 4th here:
$ sed '2,4!d' file Unix Linux Solaris
The ! operator indicates negative condition.
6. Delete the first line AND the last line of a file, i.e, the header and trailer line of a file.
$ sed '1d;$d' file Unix Linux Solaris
Multiple conditions are separated using the ‘;’ operator. Similarly, say to delete 2nd and 4th line, you can use: ‘2d;3d’.
7. Delete all lines beginning with a particular character, ‘L’ in this case:
$ sed '/^L/d' file Cygwin Unix Solaris AIX
‘^L’ indicates lines beginning with L.
8. Delete all lines ending with a particular character, ‘x’ in this case:
$ sed '/x$/d' file Cygwin Solaris AIX
‘x$’ indicates lines ending with ‘x’. AIX did not get deleted because the X is capital.
9. Delete all lines ending with either x or X, i.e case-insensitive delete:
$ sed '/[xX]$/d' file Cygwin Solaris
[xX] indicates either ‘x’ or ‘X’. So, this will delete all lines ending with either small ‘x’ or capital ‘X’.
10. Delete all blank lines in the file
$ sed '/^$/d' file Cygwin Unix Linux Solaris AIX
‘^$’ indicates lines containing nothing and hence the empty lines get deleted. However, this wont delete lines containing only some blank spaces.
11. Delete all lines which are empty or which contains just some blank spaces:
$ sed '/^ *$/d' file Cygwin Unix Linux Solaris AIX
‘*’ indicates 0 or more occurrences of the previous character. ‘^ *$’ indicates a line containing zero or more spaces. Hence, this will delete all lines which are either empty or lines with only some blank spaces.
12. Delete all lines which are entirely in capital letters:
$ sed '/^[A-Z]*$/d' file Cygwin Unix Linux Solaris
[A-Z] indicates any character matching the alphabets in capital.
13. Delete the lines containing the pattern ‘Unix’.
$ sed '/Unix/d' file Cygwin Linux Solaris AIX
The pattern is specified within a pair of slashes.
14. Delete the lines NOT containing the pattern ‘Unix’:
$ sed '/Unix\|Linux/d' file Cygwin Solaris AIX
The OR condition is specified using the | operator. In order not to get the pipe(|) interpreted as a literal, it is escaped using a backslash.
16. Delete the lines starting from the 1st line till encountering the pattern ‘Linux’:
$ sed '1,/Linux/d' file Solaris AIX
Earlier, we saw how to delete a range of lines. Range can be in many combinations: Line ranges, pattern ranges, line and pattern, pattern and line.
17. Delete the lines starting from the pattern ‘Linux’ till the last line:
$ sed '/Linux/,$d' file Cygwin Unix
$ sed '$' file Cygwin Unix Linux Solaris
$ is for the last line. To delete a particular line only if it contains the pattern AIX, put the line number in place of the $. This is how we can implement the ‘if’ condition in sed.
19. Delete the last line ONLY if it contains either the pattern ‘AIX’ or ‘HPUX’:
$ sed '$' file Cygwin Unix Linux Solaris
20. Delete the lines containing the pattern ‘Solaris’ only if it is present in the lines from 1 to 4.
$ sed '1,4' file Cygwin Unix Linux AIX
This will only delete the lines containing the pattern Solaris only if it is in the 1st four lines, nowhere else.
21. Delete the line containing the pattern ‘Unix’ and also the next line:
$ sed '/Unix/' file Cygwin Solaris AIX
N command reads the next line in the pattern space. d deletes the entire pattern space which contains the current and the next line.
22. Delete only the next line containing the pattern ‘Unix’, not the very line:
$ sed '/Unix/' file Cygwin Unix Solaris AIX
Using the substitution command s, we delete from the newline character till the end, which effective deletes the next line after the line containing the pattern Unix.
23. Delete the line containing the pattern ‘Linux’, also the line before the pattern:
$ sed -n '/Linux/;x;p;$' file | sed '/^$/d' Cygwin Solaris AIX
A little tricky ones. In order to delete the line prior to the pattern,we store every line in a buffer called as hold space. Whenever the pattern matches, we delete the content present in both, the pattern space which contains the current line, the hold space which contains the previous line.
Let me explain this command: ‘x;p;’ ; This gets executed for every line. x exchanges the content of pattern space with hold space. p prints the pattern space. As a result, every time, the current line goes to hold space, and the previous line comes to pattern space and gets printed. When the pattern /Linux/ matches, we empty(s/.*//) the pattern space, and exchange(x) with the hold space(as a result of which the hold space becomes empty) and delete(d) the pattern space which contains the previous line. And hence, the current and the previous line gets deleted on encountering the pattern Linux. The $
$ sed -n '/Linux/;1h;1!;$' file Cygwin Linux Solaris AIX
This is almost same as the last one with few changes. On encountering the pattern /Linux/, we exchange(x) and delete(d). As a result of exchange, the current line remains in hold space, and the previous line which came into pattern space got deleted.
1h;1!
$ sed -n '/Linux/;x;p;$' file | sed '/^$/d' Cygwin AIX
Sed Command to Delete a Line
Sed is a built-in Linux tool for text manipulation. The term sed stands for stream editor. Despite the name, sed isn’t a text editor by itself. Rather, it takes text as input, performs various text modifications according to instructions, and prints the output.
This guide will demonstrate how to use sed to delete a line from a text.
Sed on Linux
The full name of sed gives us a hint at its working method. Sed takes the input text as a stream. The text can come from anywhere – a text file or standard output (STDOUT). After taking the input, sed operates on it line by line.
For demonstration, here’s a simple text file I’ve generated.
Deleting line using sed
To delete a line, we’ll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.
Delete single line
The following sed command will delete the first line of the text.
Basically, to delete a line, you need the line number of the target line. Let’s remove line 5.
To delete the last line of the text file, instead of manually calculating the line number, use “$” instead.
Delete a range of line
Sed can delete a range of lines. Here, the minimum line value is 1, and the maximum line value is 5. To declare range, we’re using comma (,).
Delete multiple lines
What if the lines you desire to remove are not in a fixed range? Have a look at the following sed command. Note that we’re using a semicolon (;) as the delimiter. Essentially, each delimited option is a separate sed command.
Delete all lines except specified range
In the next example, sed will only keep the lines described by the range. Here, “!” is the negation operator, telling sed to keep the specific lines.
Delete empty lines
If there are multiple empty or blank lines in the text, the following sed command will remove all of them.
Delete lines based on pattern
Sed can search for a particular pattern and perform the specified actions on the line. We can use this feature to delete specific lines that match a pattern.
Let’s have a look at the following demonstration. Sed will remove any line that contains the string “the”.
We can also describe multiple strings to search for. Each string is delimited using the symbol “\|”.
Delete lines starting with a specific character
To denote the starting of a line, we’ll use the caret (^) symbol.
The following sed command will remove all the lines starting with a digit. Here, the character group “[:digit:]” describes all digits (0-9).
We can also describe multiple characters for a valid match. The following example will match all the lines that start with “t” and “b”.
In the next example, check out how to remove all the lines that start with an uppercase character. Here, we’re using the uppercase character group “[:upper:]”.
If the target lines have lowercase characters at the start, use the lowercase character group “[:lower:]”.
Delete lines ending with specific character
To denote the end of a line, we can use the symbol “$”. It describes the match with the last occurrence of the pattern.
In the next example, sed will delete lines ending with “e”.
Let’s try with a multiple-character search.
Deleting lines that match the pattern and the next line
We’ve already demonstrated how to delete a line if a pattern matches. We can further extend and delete the subsequent line as well.
Check out the following sed command.
Sed will match the line that contains “the” and delete the subsequent line as well.
Deleting line from the pattern match to the end
We can further extend the previous example to order sed to delete all the lines, starting from the first match of the pattern.
Here, sed will delete the line that matches the pattern “the” first and all lines afterward.
Final thought
Sed is a simple tool. However, it can do wonders, thanks to the support for regular expression. Sed also integrates seamlessly in various scripts.
This was just a short guide demonstrating one of the sed functions – deleting lines. There are tons of other things you can do with sed. For example, check out this mega guide on 50 sed examples. It’s a fantastic guide covering all the basics and many advanced sed implementations.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.