Sed Recipes

GNU Sed (stream editor) recipes and command examples

Recipes for GNU sed [1] (stream editor), which is a non-interactive command-line text editor.

sed is commonly used to filter text, i.e., it takes text input, performs some operation (or set of operations) on it, and outputs the modified text. sed is typically used for extracting part of a file using pattern matching or substituting multiple occurrences of a string within a file. Check the GNU sed manual [1] for detailed information.

Removing lines

sed recipes for removing/deleting lines from a file following one or more expressions, invoked by the -e / --expression= operator.

sed does not remove the lines from the source file by default. Use the -i option with the sed command to work on the source file. Alternatively, use redirection to save the output to a new file, e.g. sed '1d' file > newfile.

Delete empty lines or blank lines:

$ sed '/^$/d' file

Delete lines that begin with a specified character, in this case "a":

$ sed '/^a/d' file

Delete lines that end with a specified character, in this case "a":

$ sed '/a$/d' file

Delete lines where contents are written in upper case or capital letters:

$ sed '/^[A-Z]*$/d' file

Delete lines that contain the pattern "foobar":

$ sed '/foobar/d' file

Delete lines starting from a pattern till the last line

$ sed '/foobar/,$d' file



  1. GNU Sed home page
  2. GNU Sed manual