Mastering sed: Essential Stream Editing Techniques for Linux Shell
Learn how to use the powerful sed stream editor for Linux shell scripting, covering basic syntax, common commands for deleting, searching and replacing, inserting text, and printing, with practical examples that demonstrate in-place editing, redirection, and regular expression handling.
Previously we introduced the awk command; the sed command is equally important for text processing in Linux shell development.
sed stands for stream editor, a non‑interactive tool that by default does not modify the original file but outputs the edited text stream to standard output.
Like awk, sed processes text line by line, printing each line after processing until the entire file is handled.
sed can perform deletions, search‑and‑replace, additions, insertions, and reading data from other files.
Common scenarios
When editing files in shell scripts where using vi is inconvenient.
Large files that cause performance issues when opened with vi.
Regular text modifications such as global replacements.
Command syntax
sed [options] 'command' fileoptions are parameters, command is a set of commands, and file is the path of the file to edit.
Examples
1) Delete
The d command deletes lines.
Delete the first line: sed '1d' Sed.txt Redirect output to save changes: sed '1d' Sed.txt > Sed2.txt In‑place edit with -i: sed -i '1d' Sed.txt Delete a range of lines: sed '1,3d' Sed.txt Delete from the first to the last line: sed '1,$d' Sed.txt Delete the last line: sed '$d' Sed.txt Delete all lines except line 5: sed '5!d' Sed.txt Delete lines containing "abc":
sed '/abc/d' Sed.txt2) Search and replace
The s command performs substitution.
Replace the first occurrence of "abc" with "ABC": sed 's/abc/ABC/' Sed.txt Replace the second occurrence: sed 's/abc/ABC/2' Sed.txt Replace all occurrences using the g flag: sed 's/abc/ABC/g' Sed.txt Use regular expressions, e.g., replace lines starting with "abc":
sed 's/^abc/ABC/' Sed.txt3) Insert text
The i command inserts before a line, and the a command appends after a line.
Insert before line 2: sed '2 i test' Sed.txt Append after line 2: sed '2 a test' Sed.txt Insert before lines matching "abc":
sed '/abc/i\test' Sed.txt4) Print
Use the p command with the -n option to suppress automatic printing.
Print the first line: sed -n '1p' Sed.txt Print only lines where substitution occurred:
sed -n 's/abc/ABC/p' Sed.txtSigned-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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
