Can sed remove lines?

QuestionsQuestions8 SkillsProDec, 18 2025
0221

Yes, absolutely! While s is used for substitution, sed uses the d command to delete lines.

Here are the most common ways to remove lines using sed:

1. Delete a specific line

To delete the 2nd line of a file:

sed '2d' sed_test.txt

2. Delete a range of lines

To delete lines 1 through 3:

sed '1,3d' sed_test.txt

3. Delete lines matching a pattern

If you want to remove every line that contains the word "test":

sed '/test/d' sed_test.txt

4. Delete the last line

The $ symbol represents the last line in sed:

sed '$d' sed_test.txt

💡 Pro Tip:

Just like with substitution, these commands only print the result to the terminal. If you want to permanently remove the lines from the file, add the -i flag:

# This permanently deletes lines containing "test" from the file
sed -i '/test/d' sed_test.txt

Would you like to try deleting a line from your sed_test.txt file now?

0 Comments

no data
Be the first to share your comment!