Mastering Sed: Essential Commands and Real‑World Examples for Powerful Text Processing
Sed, the powerful stream editor on Linux/Unix, enables non‑interactive text manipulation such as searching, replacing, deleting, and inserting lines; this guide explains its syntax, address ranges, common commands, and provides practical examples like bulk domain replacement, comment removal, and adding copyright headers.
Introduction
Sed (Stream Editor) is a non‑interactive text‑processing tool available on Linux/Unix systems. It can perform complex editing operations—search, replace, delete, insert—directly on text streams without modifying files manually.
Basic Syntax
The general command format is: sed [options] 'operation' input_file Options : optional flags, e.g., -n suppresses automatic printing of each line.
Operation : a combination of an address (or range) and a command that defines what to do.
Input file : the file to process, or standard input.
Address Ranges
Addresses specify which lines the operation applies to. They can be a line number, a regular expression, or a combination:
Single line number (e.g., 3) – affect only that line.
Range of lines (e.g., 3,5) – affect lines 3 through 5.
Regular expression (e.g., /pattern/) – affect all lines matching the pattern.
Common Sed Commands
Substitution (replace)
Replace text using the s command:
sed 's/old/new/g' file.txt sstands for substitute; g makes the replacement global on each line.
Delete lines
sed '/pattern/d' file.txtDeletes every line that matches pattern.
Insert line before
sed '/pattern/i\New line before' file.txtInserts the specified text before each matching line.
Append line after
sed '/pattern/a\New line after' file.txtAppends the specified text after each matching line.
Show line numbers
Use = to print line numbers, or p to print the current line.
sed '=' file.txtPractical Applications
Example 1 – Bulk domain replacement
Replace all occurrences of www.oldsite.com with www.newsite.com in a configuration file:
sed -i 's/www.oldsite.com/www.newsite.com/g' config.txtThe -i option edits the file in place.
Example 2 – Delete comment lines
Remove every line that starts with # from a script:
sed '/^#/d' script.shExample 3 – Add copyright header
Insert a copyright notice at the top of each source file:
sed -i '1i\/*
* Copyright (c) 2024. All rights reserved.
*/' *.cExample 4 – Extract a column (simple case)
Although awk is better for column handling, sed can clean up the result: cut -d',' -f1 data.csv | sed 's/,//g' This extracts the first column from a CSV file and removes any stray commas.
Conclusion
Sed’s flexibility and efficiency make it indispensable for text processing tasks. Mastering its basic commands and address syntax can dramatically speed up routine operations and development workflows. The examples above cover only a fraction of its capabilities; users are encouraged to explore advanced features such as branching, loops, and label jumps for more complex scenarios.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
