Mastering sed: Essential Commands for Fast Text Processing
This guide introduces the core concepts of the Unix sed command, covering its basic syntax, common options, address ranges, editing commands, substitution patterns, useful flags, and practical one‑liners for text manipulation and system administration tasks.
sed is a widely used, simple yet powerful command‑line tool for fast text processing. It is considered a hard skill that every programmer should master because of its versatility and similarity to vim and regular‑expression syntax.
A Simple Introduction
A basic sed command consists of three parts: options, address range, and the editing command, followed by the target file. The
-foption can load a script file (advanced usage, not covered here).
Parameters
-n(or
--quiet/
--silent) suppresses the default output, printing only the results you request.
-iedits files in place, which is very dangerous because it overwrites the original file.
Using -i will modify the original file directly; be extremely careful.
Address Ranges
Line numbers can specify ranges, e.g.,
1,4selects lines 1‑4. Other examples:
<code>sed -n '5p' file</code><code>sed -n '2,5p' file</code><code>sed -n '1~2p' file</code><code>sed -n '2~2p' file</code><code>sed -n '2,+3p' file</code><code>sed -n '2,$p' file</code>Regular‑expression ranges are also supported:
<code>sed -n '/sys/,+3p' file</code><code>sed -n '/^sys/,/mem/p' file</code>Editing Commands
The most common command is
p(print). Others include:
p prints matching lines. d deletes matching lines (requires dropping -n ). w writes matching lines to another file.
Less frequently used commands such as
a,
i, and
care omitted for brevity.
<code>sed -n '2,5p' file</code><code>sed '2,5d' file</code><code>sed -n '2,5w output.txt' file</code>Substitution Mode
The
scommand replaces matched text. Its basic form is
s/pattern/replacement/flags. Flags include
g(global replace),
p(print when used with
-n),
w(write changed lines),
i(ignore case), and
e(execute the result as a command).
<code>sed -n 's/a/b/gipw output.txt' file</code><code>sed 's/^/ls -la/e' file</code>The special placeholder
&represents the entire matched text, allowing constructions such as:
<code>sed 's/.*/"&"/' file</code>Alternative Delimiters
Because slashes often appear in patterns, you can use other delimiters:
<code>sed '/aaa/s@/etc@/usr@g' file</code><code>sed '/aaa/s|/etc|/usr|g' file</code>In‑Place Editing with Backup
Use
-i.bakto edit a file while keeping a backup:
<code>sed -i.bak 's/a/b/' file</code>Practical One‑Liners
Print lines longer than 50 characters:
<code>sed -n '/^.{50}/p'</code>Count word occurrences:
<code>sed 's/ /\n/g' file | sort | uniq -c</code>Remove comments from Python files:
<code>find ./ -name "*.py" | xargs sed -i.bak '/^[ ]*#/d'</code>Show specific line ranges:
<code>sed -n -e '5,7p' -e '10,13p' file</code>Extract only IP addresses:
<code>ip route show | sed -n '/src/p' | sed -e 's/ */ /g' | cut -d' ' -f9</code>sed also offers an
x(exchange) command and can even be used to implement games like Sokoban ( GitHub link ).
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.