← 返回命令列表

Linux command

sed 命令

文本

涉及管道、覆盖或删除,执行前请先确认路径和参数。

常用示例

Substitute

[command] | sed 's/apple/mango/g'

Replace in-place

sed -i 's/apple/mango/g' [path/to/file]

Example

[command] | sed -e 's/apple/mango/g' -e 's/orange/lime/g'

Example

[command] | sed 's#////#____#g'

Delete lines

sed -i.orig '1,5d' [path/to/file]

Print only

[command] | sed -n '1p'

Insert

sed -i '1i\your new line text' [path/to/file]

Delete blank lines

sed -i '/^[[:space:]]*$/d' [path/to/file]

说明

sed (stream editor) is a powerful text processing tool that performs basic transformations on input streams (files or piped data). It reads input line by line, applies specified editing commands, and writes to standard output. Common operations include search and replace (s///), deletion (d), insertion (i), and printing (p). sed uses regular expressions for pattern matching and supports both basic and extended regex syntax. Address ranges (line numbers or patterns) can target specific lines.

参数

-i_suffix_, --in-place=_suffix_
Edit files in place; optionally create backup with suffix
-e _script_, --expression=_script_
Add script commands to execute
-f _file_, --file=_file_
Read script from file
-n, --quiet, --silent
Suppress automatic printing; only print when p command used
-r, -E, --regexp-extended
Use extended regular expressions
-s, --separate
Treat files as separate rather than single stream
-z, --null-data
Separate lines by NUL characters
--sandbox
Block input/output commands (e/w/r); use for untrusted scripts
--posix
Disable all GNU extensions for POSIX compliance
--debug
Annotate program execution

FAQ

What is the sed command used for?

sed (stream editor) is a powerful text processing tool that performs basic transformations on input streams (files or piped data). It reads input line by line, applies specified editing commands, and writes to standard output. Common operations include search and replace (s///), deletion (d), insertion (i), and printing (p). sed uses regular expressions for pattern matching and supports both basic and extended regex syntax. Address ranges (line numbers or patterns) can target specific lines.

How do I run a basic sed example?

Run `[command] | sed 's/apple/mango/g'` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -i_suffix_, --in-place=_suffix_ do in sed?

Edit files in place; optionally create backup with suffix