Lake Denman

Delete a Single Line From a File

Let’s say that you have an arbitrary file:

echo "one\ntwo\nthree\nfour" > foo
cat foo

That will produce the following text file:

one
two
three
four

Now, this is the magic sauce to delete a specific line without opening an editor:

sed -i '1d' foo

Read the text file again and see the difference:

two
three
four

Deleting a single line with sed turns out to be very easy.

Home