CmdKeys LogoCommand Quiz

Sed Command Practice

sed reads a stream, applies an address plus a command, and writes the result. This track drills s///g, range delete, -n print, and -i in-place edits — the scripts you type in a pipeline, not a GUI find-and-replace. Grep selects lines; sed changes them. Awk can wait for its own field track.

Command Reference

delete

CommandDescription
sed '<num>,<num>d' <file>Delete a start-end line range and print the rest.
sed '$d' <file>Delete the last line and print the rest.
sed '/^$/d' <file>Delete empty lines and print the rest.
sed '/^#/d' <file>Delete lines that start with # and print the rest.
sed '/^<pattern>/d' <file>Delete lines whose start matches a pattern and print the rest.

print

CommandDescription
sed -n '<num>,<num>p' <file>Print only a start-end line range.
sed -n '/<pattern>/p' <file>Print only lines that match a pattern.
sed -n '<num>p' <file>Print only one numbered line; suppress the default print of every line.
sed -n '/<pattern>/!p' <file>Print only lines that do not match a pattern.
sed -n '/<pattern>/Ip' <file>Print matching lines, ignoring case in the search.
sed -n '$=' <file>Print the last line number (a line count) and nothing else.

replace

CommandDescription
sed -i 's/<old>/<new>/g' <file>Edit the named file in place, replacing every match on each line.
sed 's/<old>/<new>/g' <file>Replace every match of a string on each line and print the result.
sed 's/<old>/<new>/2' <file>Replace only the second match of a string on each line.
sed '<num>s/<old>/<new>/' <file>Replace the first match on one numbered line only.
sed '/<pattern>/s/<old>/<new>/' <file>Replace the first match only on lines that match a pattern.
sed -i.bak 's/<old>/<new>/g' <file>Edit the file in place after writing a .bak backup, replacing every match.
sed -E 's/<pattern>//g' <file>Delete every extended-regex match on each line (for example a digit run).

script

CommandDescription
sed -e 's/<old>/<new>/g' <file>Run one substitute script passed with -e.
sed -f <script> <file>Run commands from a sed script file against an input file.
sed -e 's/<old>/<new>/g' -e 's/<pattern>/<text>/g' <file>Run two substitute scripts in order on the same input.
sed -n -e '<num>,<num>p' -e '<num>,<num>p' <file>Print two line ranges and suppress the default print of every line.

insert

CommandDescription
sed '<num>i <text>' <file>Insert a text line before a numbered line.
sed '$a <text>' <file>Append a text line after the last line of the file.
sed '<num>a <text>' <file>Append a text line after a numbered line.
sed '/<pattern>/i <text>' <file>Insert a text line before every line that matches a pattern.

Key Use Cases

  • Replace every occurrence of a token in a file and print the result
  • Edit a config in place and keep a .bak copy
  • Delete blank lines or comment lines from a stream
  • Print only lines that match a pattern, or only a line range
  • Insert a banner before matching lines or append text at EOF
  • Strip digits with an extended regex (-E)

Frequently Asked Questions

How is sed different from grep?

grep prints lines that match. sed transforms the stream: substitute, delete, insert, or print a subset. sed -n '/pat/p' can look like grep, but s/// and -i do not.

What does the g in s/old/new/g do?

Without g, only the first match on each line is replaced. g replaces every match on the line. A number like 2 replaces only the second match.

Does sed -i change the file I named?

Yes. GNU -i edits in place. -i.bak writes a backup first. Without -i, sed prints to stdout and leaves the file alone.

When do I need sed -E?

Basic sed regex treats + and grouping differently. -E (--regexp-extended) lets you write [0-9]+ without extra backslashes.

Ready to master Sed commands?

Test your muscle memory with our spaced-repetition quiz system. Free forever.

Start Practice Now →