Sed/Examples
More actions
For the main page, visit sed.
This page showcases different example usecases with sed, with varying complexity.
Replace `backticks`
with <code>
Tags
Suppose the working directory contains the file file.txt
:
`sed` is a very powerful text processor. It is commonly used to perform text transformations. `sed` is inspired by `grep`.
First, the texts wrapped around backticks can be obtained using the
-E
flag, which enables extended regular expression[1] (aka. regex).
$ sed -E 's/`.+`/FOUND/g' file.txt FOUND is a very powerful text processor. It is commonly used to perform text transformations. FOUND.
The g
flag (global) performs the substitution infinitely many times instead of just once for each line.
Notice the last line is not matched correctly since .+
is greedily evaluated[2].
This can be resolved by using [^`]
, which matches all characters except `
, instead of .
which matches all characters:
$ sed -E 's/`[^`]+`/FOUND/g' file.txt FOUND is a very powerful text processor. It is commonly used to perform text transformations. FOUND is inspired by FOUND.
Next, we add regex group 1 to the inner texts wrapped inside the backticks,
and set the replacement text to <code>\1</code>
.
\1
expands to the content of regex group 1.
$ sed -E 's/`([^`]+)`/<code>\1</code>/g' file.txt sed: -e expression #1, char 23: unknown option to `s'
The error occurs because the replacement text contains the character /
,
which is used as the separator for the s
command.
The desired result can be obtained by escaping it using a backslash:
$ sed -E 's/`([^`]+)`/<code>\1<\/code>/g' file.txt <code>sed</code> is a very powerful text processor. It is commonly used to perform text transformations. <code>sed</code> is inspired by <code>grep</code>.
Alternatively, we can change the separator to @
:
$ sed -E 's@`([^`]+)`@<code>\1</code>@g' file.txt <code>sed</code> is a very powerful text processor. It is commonly used to perform text transformations. <code>sed</code> is inspired by <code>grep</code>.
Finally, we can use the -i
flag to edit the file directly:
$ sed -E 's@`([^`]+)`@<code>\1</code>@g' -i file.txt
FizzBuzz
FizzBuzz is a famous problem that involves 4 rules:
- if a number is divisible by 15, print
FizzBuzz
; - if a number is divisible by 3, print
Fizz
; - if a number is divisible by 5, print
Buzz
; - otherwise, print the number itself.
In this example, FizzBuzz is performed on the first 50 positive integers, which are prepared with seq 50
. The numbers can be further processed by checking whether the line number is a multiple of 15, 3, or 5:
sed -E '
15~15{s@.+@FizzBuzz@; b}
3~3{s@.+@Fizz@; b}
5~5s@.+@Buzz@'
~15
performs the specified command following itself every 15 lines. The 15
before the ~
sign indicates which line to start counting. In this case, s@.+@FizzBuzz@; b
is performed every 15 lines starting with the 15th line.
s@.+@FizzBuzz@
performs a substitution. With the -E
flag, the regex .+
can be used to match for the entire line. The entire line is then replaced with FizzBuzz
. Finally, the b
command (b
stands for branch) tells sed to skip the remaining commands and process the next line.
The same is done for line numbers divisible by 3 and 5. Note that for 5~5s@.+@Buzz@
, b
is no longer needed since all conditions have already been processed.
The commands can be further improved by using c
(which stands for change):
sed '15~15c FizzBuzz
3~3c Fizz
5~5c Buzz'
Note the lack of -E
since regexes are no longer in use.
And finally, pr -5t
prints out the results in 5 columns. The full command is now:
seq 50 | sed -E '15~15c FizzBuzz
3~3c Fizz
5~5c Buzz' | pr -5t
The output is:
1 11 Fizz 31 41 2 Fizz 22 32 Fizz Fizz 13 23 Fizz 43 4 14 Fizz 34 44 Buzz FizzBuzz Buzz Buzz FizzBuzz Fizz 16 26 Fizz 46 7 17 Fizz 37 47 8 Fizz 28 38 Fizz Fizz 19 29 Fizz 49 Buzz Buzz FizzBuzz Buzz Buzz
Renaming Files
Multiple files can be renamed if the new name can be easily systematically determined from the old name:
ls *.txt | sed 's/\(.*\)\.txt/mv & \1_old.txt/' | sh
The commands could also be executed within sed:
ls *.txt | sed 's/\(.*\)\.txt/mv & \1_old.txt/e'
References
- ↑ sed(1) - Linux manual page, man7.org, 2022 (Accessed: 2025-06-20)
- ↑ Regular Expression Tutorial Part 5: Greedy and Non-Greedy Quantification, Andrew Johnson, 2001 (Accessed: 2025-06-20)