03. Deleting lines and saving processed files

To delete lines from our file, we use the d command instead of the p command as we saw earlier.

Notice that for deleting, we no longer need the -n option since sed won't print out the deleted lines (once they are deleted there is nothing to be printed). Thus the output will only be the lines that were not deleted.

Don't forget an address with deleting!

Make sure to specify an address! Without one, sed will delete all lines, which isn't you wouldn't want.

Let's see what happens when we delete the third and fourth lines:

$ sed '3,4d' oneOS.txt
One OS to rule them all, One OS to find them. In the bright land of Linux, Where the hackers play.

Saving to a new file

Notice that using the delete operation does not save the output to a new file. To save to a new file, simply direct the output to a new file with the redirection symbol (>).

$ sed '1~2d' BSD > everyOther.txt

Editing and Saving in-place

To edit the file and save directly, we can use the -i option. Note that you may lose data if used incorrectly, so be sure to create a backup prior to editing! To create a .bak file directly within the command, tack on the .bak (or any other extension) after the -i option.

$ sed -i.bak 'd' oneOS.txt
# Woops! Accidentally deleted all lines, but good thing we made a backup!

The "-i" in-place option varies between implementations of sed. GNU sed allows for it while FreeBSD's sed requires an extension parameter. OpenBSD's sed does not support the "-i" option at all.

Deleting lines with a regex match

To delete lines that contain a specific match to a regex, embed the regular expression between two slashes, before the option.

This command would delete the lines that start with an uppercase vowel.

$ sed '/^[AEIOU]/d' oneOS.txt
Where the hackers play.

More fun examples

Here are a list of more fun deletions that you may find helpful:

1. Deleting first and last lines

To specify multiple delete commands, separate them with a semi-colon (;).

$ sed '1d;$d' oneOS.txt
# Deletes first and last line
One OS to find them. One OS to call them all, And in salvation bind them. In the bright land of Linux,

2. Deleting every other line

To delete every other line, refer back to the ~ address operator, which takes every multiple starting from an integer. Note that the ~ syntax is only GNU-specific and won't work in BSD flavors of sed.

$ sed '1~2d' oneOS.txt
# Removes all odd number lines (only on GNU)
One OS to rule them all, One OS to call them all, In the bright land of Linux,

3. Removing all empty lines

Use a simple regular expression to rid of all empty lines. Or rid the inverse of all lines that have content.

$ sed '/^$/d' oneOS.txt
$ sed '/./!d' oneOS.txt

4. Deleting from a pattern to end of file

$ sed '/salvation/,$d' oneOS.txt
One OS to rule them all, One OS to find them. One OS to call them all,

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources