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 -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:
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' fileRegular‑expression ranges are also supported:
sed -n '/sys/,+3p' file</code><code>sed -n '/^sys/,/mem/p' fileEditing 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.
sed -n '2,5p' file</code><code>sed '2,5d' file</code><code>sed -n '2,5w output.txt' fileSubstitution 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).
sed -n 's/a/b/gipw output.txt' file</code><code>sed 's/^/ls -la/e' fileThe special placeholder & represents the entire matched text, allowing constructions such as:
sed 's/.*/"&"/' fileAlternative Delimiters
Because slashes often appear in patterns, you can use other delimiters:
sed '/aaa/s@/etc@/usr@g' file</code><code>sed '/aaa/s|/etc|/usr|g' fileIn‑Place Editing with Backup
Use -i.bak to edit a file while keeping a backup:
sed -i.bak 's/a/b/' filePractical One‑Liners
Print lines longer than 50 characters: sed -n '/^.{50}/p' Count word occurrences: sed 's/ /\n/g' file | sort | uniq -c Remove comments from Python files:
find ./ -name "*.py" | xargs sed -i.bak '/^[ ]*#/d'Show specific line ranges: sed -n -e '5,7p' -e '10,13p' file Extract only IP addresses:
ip route show | sed -n '/src/p' | sed -e 's/ */ /g' | cut -d' ' -f9sed also offers an x (exchange) command and can even be used to implement games like Sokoban ( GitHub link ).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
