Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

How sed Works

sed reads its input one line at a time, runs each command on that line, and prints the result. The input file stays unchanged unless you ask for an in-place edit with -i. The basic form:

sed 'COMMANDS' file.txt
some-command | sed 'COMMANDS'

A command can start with an address that picks the lines it applies to: a line number, a range such as 10,20, $ for the last line, or a regular expression between slashes. Without an address, the command applies to every line. Ubuntu, Debian, AlmaLinux, and Rocky Linux all ship GNU sed, so the examples behave the same on each of them.

Print Selected Lines

By default sed prints every line. The -n option turns that off, and the p command prints only the lines you address:

sed -n '5p' file.txt          # line 5
sed -n '10,20p' file.txt      # lines 10 to 20
sed -n '$p' file.txt          # the last line
sed -n '/error/p' app.log     # lines that contain "error"

Without -n, the addressed lines appear twice: once from p and once from the normal output.

Delete Lines

The d command removes lines from the output:

CommandRemoves
sed '3d'Line 3
sed '2,4d'Lines 2 to 4
sed '$d'The last line
sed '/^#/d'Comment lines
sed '/^$/d'Empty lines
sed '/^\s*$/d'Blank lines with spaces

Replace Text

The s command substitutes text. This replaces every "http:" with "https:" on each line:

sed 's/http:/https:/g' links.txt

Without the final g, only the first match on each line changes. Other delimiters, case-insensitive matching, special characters in the replacement, and replacing across many files are covered in sed replace: find and replace text in files.

Insert, Append, and Change Lines

i inserts a line before the addressed line, a appends one after it, and c replaces the whole line:

sed '1i # Managed by the deploy script' app.conf
sed '/^\[mysqld\]/a max_connections = 200' my.cnf
sed '$a last line' file.txt
sed '/^level=/c level=debug' app.conf

This one-line form of i, a, and c is a GNU sed extension, like \s in the delete table above, so both work on all the distributions named above.

Edit a File in Place

A warning first: -i overwrites the file, and a wrong expression can empty or damage it. Run the command without -i, read the output, and only then write the change with a backup copy:

sed 's/8080/8081/' app.conf            # preview only
sed -i.bak 's/8080/8081/' app.conf     # edit, keep app.conf.bak
diff app.conf.bak app.conf             # see what changed

With -i.bak, sed keeps the original as app.conf.bak. Plain -i keeps no copy. For a service's configuration, run the service's own check before a restart, for example sudo nginx -t.

Several Commands at Once

Give each command its own -e, or separate them with semicolons:

sudo sed -e '/^#/d' -e '/^$/d' /etc/ssh/sshd_config
sed 's/foo/bar/g; /^$/d' file.txt

The first line shows a configuration file without its comments and empty lines, which makes the active settings easy to read; sudo is there because some systems let only root read that file. For a longer list, put the commands in a file, one per line, and run sed -f script.sed file.txt.

Extended Regular Expressions

By default sed uses basic regular expressions, where +, ?, |, and parentheses need a backslash to act as operators. With -E, they work without one:

sed -E 's/([0-9]+)\.([0-9]+)/\2.\1/' versions.txt
sed -E '/^(DEBUG|TRACE)/d' app.log

The first command swaps two numbers around a dot; \1 and \2 refer to the groups in parentheses. The second drops debug and trace lines from a log.

Ranges Between Two Patterns

An address can run from one pattern to another. This prints one section of an INI-style file, from its header down to the next header line, both included:

sed -n '/^\[database\]/,/^\[/p' config.ini

The same range with d instead of p deletes the block. A range starts again each time the first pattern matches, so check the output when the start pattern appears more than once.

sed, awk, or grep?

Use grep to find lines, sed to change them, and awk when the work depends on columns, such as printing the third field or adding up a column. The three combine well in pipes, for example grep -v '^#' app.conf | sed '/^$/d'.

Was this answer helpful?
Loading...