Mastering the Linux sed Command: A Powerful Text Manipulation Tool
Table of contents
No headings in the article.
Introduction: In the world of Linux command-line tools, there are many powerful utilities that can make your life as a developer or sysadmin easier. One such tool is sed
, short for "stream editor." sed
is a versatile and efficient command-line tool used for performing text transformations and manipulations on files and streams. In this blog post, we will explore the fundamentals of sed
and provide you with a handy cheat sheet to reference while working with this powerful tool.
Understanding sed
: At its core, sed
operates by reading text from a file or standard input, applying a set of commands to the text, and then producing the modified output. The commands in sed
are expressed as a sequence of instructions and can perform various operations like search and replace, deletion, insertion, and more. The power of sed
lies in its ability to process large amounts of text quickly and efficiently.
Cheat Sheet Breakdown: To help you grasp the essentials of sed
, we have prepared a convenient cheat sheet. Let's break down the key components:
Common Options:
-i <extension>
: Edit files in-place, optionally creating a backup with the given extension.-e <script>
: Add the script containing commands to be executed.-n
: Suppress automatic printing.-r
: Use extended regular expressions.
Common Commands:
s/<pattern>/<replacement>/
: Substitute a pattern with the specified replacement./pattern/
: Print lines matching the pattern./dpattern/
: Delete lines matching the pattern.i\
: Insert text before the current line.a\
: Append text after the current line.c\
: Change the entire line with new text.n
: Read the next line into the pattern space.
Modifiers:
Placed after the last delimiter, these modifiers enhance the behavior of commands:
g
: Replace all occurrences on a line.n
: Replace the nth occurrence on a line.p
: Print only if substitution occurs.w <file>
: Write output to a file.q
: Quit processing after executing the script.
Common Patterns and Metacharacters:
Understand the patterns and metacharacters used in
sed
:.
: Represents any single character.*
: Denotes zero or more occurrences of the previous character.^
: Matches the start of a line.$
: Matches the end of a line.[abc]
: Matches any character listed inside the brackets.[^abc]
: Matches any character NOT listed inside the brackets.\
: Escapes the next character.\d
: Matches any digit (0-9).\w
: Matches any word character (a-z, A-Z, 0-9, _).
Examples: Now let's illustrate the power of sed
with a few practical examples:
Replace All Occurrences:
sed 's/apple/orange/g' file.txt
This command substitutes all occurrences of "apple" with "orange" in the file
file.txt
.Delete Lines:
sed '/pattern/d' file.txt
The above command deletes all lines containing the specified "pattern" from
file.txt
.In-Place Editing with Backup:
sed -i.bak 's/word/WORD/g' file.txt
This command
edits file.txt
in-place, replacing all occurrences of "word" with "WORD" and creating a backup file file.txt.bak
.
Print Specific Line Range:
sed -n '5,10p' file.txt
The above command prints lines 5 to 10, inclusive, from
file.txt
.Executing Multiple Commands:
sed '/pattern/{s/foo/bar/g; s/baz/qux/g}' file.txt
This example executes multiple commands on lines matching "pattern." It replaces "foo" with "bar" and "baz" with "qux" on those lines.
Conclusion: The sed
command is a powerful text manipulation tool that provides a wide range of capabilities for processing and transforming text. With the help of our cheat sheet and examples, you can leverage sed
to streamline your text editing tasks in Linux. Experiment with different commands, patterns, and modifiers to unlock the full potential of sed
and enhance your productivity in the Linux command line.