How to remove a row from text file using a sed command

Below you can find an example how to remove a row from a text file using
sed command and bash shell. Here is a content of our sample file:

$ cat example.txt 
AAAAAAAAAA
BBBBBBBBBB
CCCCCCCCCC
DDDDDDDDDD
EEEEEEEEEE

Next, we are going to remove a second row from the above
example.txt file:

$ sed 2d example.txt 
AAAAAAAAAA
CCCCCCCCCC
DDDDDDDDDD
EEEEEEEEEE

Futhermore, we can also remove multiple rows from this file. For example let’s
remove all rows from 2 to 4:

$ sed 2,4d example.txt
AAAAAAAAAA
EEEEEEEEEE

We can also remove all lines matching a certain regular expression. In the next
example we are going to remove line which contains at least one C
character:

$ sed '/C/'d example.txt
AAAAAAAAAA
BBBBBBBBBB
DDDDDDDDDD
EEEEEEEEEE

To save the change into a existing file use -i option. This way the
sed command will overwrite your original file:

$ sed -i '/C/'d example.txt

or simply redirect the output to a new file:

$ sed '/C/'d example.txt > example2.txt

Additional examples:

Remove an empty lines:

$ sed '/^$/d' example.txt

Remove a last line:

$ sed '$d' example.txt

Remove a first line:

$ sed 1d example.txt