Technical skill

Linux command: sed

Make the same text change across many lines

Suppose a notes file uses one spelling and you want another. sed reads text, applies editing commands, and writes the result to standard output. It can read a file or receive text from another command. By default it works a line at a time, using a temporary work area called the pattern space. It normally does not load the whole file into that area.

The s command replaces text. Its shape is s/regular-expression/replacement/flags; a regular expression is a pattern that can match text. For example:

sed 's/colour/color/g' notes.txt

This prints changed text and leaves notes.txt alone. The g flag replaces every match on each selected line. Without it, sed replaces only the first match on a line. The default pattern syntax is basic regular expressions. GNU sed and many other implementations accept -E for extended syntax, but check the target system's manual when portability matters.

A sed command can act on every input line or only on lines chosen by an address. An address can be a line number, regular expression, or range. For example, sed -n '/ERROR/p' app.log uses -n to stop automatic printing and p to print lines matching ERROR. sed -n '5,8p' notes.txt prints lines 5 through 8, including both ends. Regular-expression addresses can select lines by their text, but sed's line-based method is not a parser for nested formats such as arbitrary JSON or HTML.

To save changed output while keeping the source intact, send it to a different file:

sed -E 's/[[:space:]]+$//' notes.txt > notes.trimmed.txt

This removes trailing whitespace from each output line. Do not redirect into the same file that sed is reading: the shell empties the output file before sed begins reading it. Inspect the new file before replacing the original.

GNU sed provides -i for in-place editing, with an optional backup suffix in GNU syntax. Other implementations differ, and -i is not specified by POSIX. A backup can help recover a source file, but it does not make a broad replacement safe. Try the expression on a short copy first.

Quote the sed script so the shell does not expand characters such as $ or interpret backslashes before sed receives them. A different delimiter can help when paths contain /, as in s|old/path|new/path|g. The delimiter changes; the regular-expression and replacement rules remain. Check which lines the command selects, what it changes, and where its output goes.

Interactive exercises

Materials

Skillsets
Skillset Skyeng Senior PlatOps preview
image descriptionSkyeng Senior PlatOps
by Artem Naumenko (slack2)
7 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset PostgreSQL DBA preview
image descriptionPostgreSQL DBA
by Artem Naumenko (fb)
1630 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Skillset Skyeng linux system administrator preview
image descriptionSkyeng linux system administrator
by Vitaliy Okulov
3 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Skyeng Ops (for DevOps) preview
image descriptionSkyeng Ops (for DevOps)
by Roman Burnashev
2 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful