Using sed
Command
Read the provided text file and remove lines containing string using sed
command. We will not modify the input file (file.txt
) but display the output on the Bash console. We can use -i
option in case we want to modify input file
1 2 3 4 5 6 7 8 |
This is line number one. It's line two. Here is line number three. Are you looking for line four? Are you also searching for line five? Forget them, and see it is line six. |
1 2 3 |
sed '/number/d' ./file.txt |
1 2 3 4 5 6 |
It's line two. Are you looking for line four? Are you also searching for line five? Forget them, and see it is line six. |
Let’s break down command to understand it better.
- The sed is a stream editor command; we used it to manipulate the specified text files.
- The /number/ pattern matched all the lines containing the pattern.
- The /d is a sed command, instructing it to delete the matched lines.
- The file.txt was the input file name
Here /number/
is pattern here and d
operator is used for deleting the line which contains the pattern.
You can replace number
with particular text for which you want to remove line containing that particular text.
1 2 3 |
sed '/Pattern to match/d' ./file.txt |
We can use the -i
option with the sed
command to directly modify the input file (file.txt
).
1 2 3 4 5 6 7 8 |
This is line number one. It's line two. Here is line number three. Are you looking for line four? Are you also searching for line five? Forget them, and see it is line six. |
1 2 3 |
sed -i '/number/d' ./file.txt |
1 2 3 4 5 6 |
It's line two. Are you looking for line four? Are you also searching for line five? Forget them, and see it is line six. |
We can use -i.bak
with the sed
command to create a backup of the input file (file.txt
) first and then modify it directly. The backup file will be named as inputFileName.Extension.bak
1 2 3 4 5 6 7 8 |
This is line number one. It's line two. Here is line number three. Are you looking for line four? Are you also searching for line five? Forget them, and see it is line six. |
1 2 3 |
sed -i.bak '/number/d' ./file.txt |
1 2 3 4 5 6 7 8 |
This is line number one. It's line two. Here is line number three. Are you looking for line four? Are you also searching for line five? Forget them, and see it is line six. |
1 2 3 4 5 6 |
It's line two. Are you looking for line four? Are you also searching for line five? Forget them, and see it is line six. |
Further reading:
If we don’t want to modify the input file (file.txt
) then we can store the updated content in a separate output file (output.txt
) using the redirection operator (>
).**
1 2 3 |
sed '/number/d' ./file.txt > output.txt |
1 2 3 4 5 6 |
It's line two. Are you looking for line four? Are you also searching for line five? Forget them, and see it is line six. |
The sed
is a command-line utility for stream editing. In all the above examples, we use the sed
command to read the input file and process it line by line. Also, the /number/
is the pattern to be matched while the d
is used to delete the matched pattern.
That’s all about sed remove line containing string.