sed - Linux
Overview
The sed
command, short for stream editor, is a powerful utility for parsing and transforming text in Unix and Unix-like operating systems. sed
is primarily used for filtering and transforming text from files or input streams. Its primary purpose lies in searching, finding, replacing, inserting, and deleting strings and lines. sed
is most effective for its ability to efficiently process text without opening an interactive text editor.
Syntax
The basic syntax of the sed
command is:
sed [options]... {script-only-if-no-other-script} [input-file]...
options
: Command-line options that modify the behavior ofsed
.script
: Series of commands thatsed
will execute.input-file
: File(s) thatsed
reads. If none specified,sed
reads from the standard input.
Options/Flags
Here is a list of commonly used options in sed
:
-e script
: Add the script to the commands to be executed.-f script-file
: Add the contents of script-file to the commands to be executed.-i[SUFFIX]
: Edit files in-place (makes backup if extension supplied).-n
: Suppress automatic printing;sed
will only output lines explicitly told to print.
Typical use cases:
- Use
-e
to execute multiple editing commands. - Use
-i
to directly modify a file instead of outputting the changed version to the standard output.
Examples
Example 1: Simple Search and Replace
Replace all occurrences of ‘hello’ with ‘world’ in a file:
sed 's/hello/world/g' filename.txt
Example 2: In-place Editing
Modify a file in-place and replace ‘foo’ with ‘bar’:
sed -i 's/foo/bar/g' filename.txt
Example 3: Print Specific Lines
Print the first 10 lines of a file:
sed -n '1,10p' filename.txt
Example 4: Delete Lines
Delete line 3 from a file:
sed '3d' filename.txt
Example 5: Advanced Pattern Use
Change “red” to “blue” only for lines containing “color”:
sed '/color/s/red/blue/' filename.txt
Common Issues
-
Modifying Files Directly: When using the
-i
option without specifying a backup, original files are overwritten, leading to data loss if not used carefully. Always consider backing up files before using-i
. -
Complex Regular Expressions:
sed
uses by default a basic type of regular expression (BRE), and some expected regex features will not work unless you use extended regular expressions with the-E
flag.
Integration
sed
can be very powerful when used in combination with other tools like awk
for text processing, or within shell scripts to automate batch editing tasks. Here’s an example of sed
used with find
:
find . -type f -name "*.txt" -exec sed -i 's/old/new/g' {} +
This command finds all .txt
files in the current directory and subdirectories, replacing ‘old’ with ‘new’ in each file.
Related Commands
awk
: A text-processing language that is more full-featured but also more complex.grep
: Command-line tool for searching plain-text data sets for lines that match a regular expression.tr
: Translates or deletes characters from stdin, used for basic transformations.
For more in-depth tutorials or examples, refer to the GNU sed official documentation.