Fundamentals 10 min read

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.

Efficient Ops
Efficient Ops
Efficient Ops
Mastering sed: Essential Commands for Fast Text Processing

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

-f

option 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.

-i

edits 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,4

selects 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

c

are 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

s

command 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

&amp;

represents the entire matched text, allowing constructions such as:

<code>sed 's/.*/"&amp;"/' 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.bak

to 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 ).

Command-lineunixRegextext processingshell scriptingsed
Efficient Ops
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.