An Introduction of Sed
Genreral Usage
sed 选项 指令 *文件
Process content in 文件 with 指令 and output the content processed to
stdout. The original file will remain unchanged. If multiple 文件 is
given, by default sed
will concatenate the output.
sed
only supports BRE. But you can use some ERE
characters by escaping them.
Options
Options | Description |
---|---|
-e 指令 | Add 指令 to processing commands. It is the way to process 文件 with multiple commands. |
-f 文件 | Add sed commands in 文件 to processing commands |
-n | Do not generate output automatically. Use
p flag instead |
-s | Treat different files separately. |
--posix | Disable GNU entensions. |
-r or --regexp-extended | Use extended regular expressions. |
--sandbox | Disable e or r
or w commands |
-u or --unbuffered | flash buffer more often. |
-z or --null-data | separate lines wwith NUL instead of "" |
If neither -e
nor -f
(and their
equivalents) is given, sed
will regard the first parameter
as the only script. ****
Grammar
Different commands write in one line should be separated with a semicolon.
By default, sed
use / to separate commands and
parameters, which is called the delimiter.
sed
allows expression groups and specified matching
length. You just need to escape the "()" and "{}". You can use the to
represent the 数字 th group in the second parameter of the subtitute
command.
Commands | Effect | Flag |
---|---|---|
s / 查找 / 替换 / | Substitute every 查找 with 替换 in the pattern space. | 数字: The command will only replace 数字 th
occurrence in every pattern space g: will replace all occurrence. p: print the origin content wherever substitue occurs. w 文件: write the result to 文件. |
d | Delete the pattern space | |
i 内容 | Insert 内容 before the pattern space. Use "\" and newline to insert multiple lines. | |
a 内容 | Append 内容 to the pattern space. Use "\" and newline to append multiple lines. | |
c 内容 | Change the pattern space into 内容. | |
t / 键 / 值 | Transform all chars in 键 into corresponding 值. If 键 does not have the same. | |
p | Print the pattern space | |
= | Print the pattern space with their line numbers. | |
l | List all characters in the pattern space. All unprintable characters will use C-style notation or octal numbers preceeded by a backslash. | |
w 文件 | Write the pattern space into 文件. | |
r 文件 | Load the content in 文件 before the pattern space. | |
n | Skip processing the current pattern space and move to next line as the new pattern space. | |
N | Take the next line into the pattern space. If no next line, the process will stop. | |
D | Delete all characters forward the first "" in the pattern space. | |
P | Print all characters forward the first "" in the pattern space | |
b 标签 | Jump to 标签 in the script. Use : 标签名 inside the script to define a label. The default is the end of the script. | |
t 标签 | If the previous substitute command is executed successfully, jump to 标签 like branch command. The default is the end of the script. | |
& | Only used in the second parameter of the substitute command. It represents the matched content in the first parameter. |
If you add a "!" right after the line addressing, this command will take effect on all lines except in the pattern space.
Pattern Space.
Pattern space is what sed
processes with given commands
as a whole.
By default, the pattern space is one line.
When all commands have been executed on the current pattern space. The next line will become the new pattern space.
Hold Space
When processing the pattern space, it is used to store the lines temporarily. There are some commands to operate on the hold space.
by default, the hold space is a blank line.
Commands | Effect |
---|---|
h | Copy the pattern space to the hold space(overwrite) |
H | Add the pattern space to the hold space. |
g | Copy the hold space to the pattern space.(overwrite) |
G | Add the hold space to the pattern space. |
x | Exchange contents in both the space. |
Line Addressing
In order to make commands take effect only in specified lines. You should give link addressing at the start of the command. There are two kinds of line addressing.
If you want multiple commands to have the same line addressing, you can bracket those commands with angle braces.
1. Line Number
Forms | Descriptions |
---|---|
数字 | Only affact the 数字 th line. |
数字 1, 数字 2 | Only affact lines from 数字 1th to 数字 2th. Can not use before insert and append command |
You can use "$" to represent the last line.
2. Text Matching
Add /正则表达式/
to the start of the command to make
commands take effect on lines matching 正则表达式.
Add /正则表达式1/, /正则表达式2/
to the start of the
command to delete all lines between lines matching 正则表达式 1 and lines
matching 正则表达式 2. If there's no more lines matching 正则表达式 2, it
will delete all lines behind the line matching 正则表达式 1.
Delimiter
By default, the delimiter of commands s
and
t
and text matching is /. But for the sake of
convinence, you can use other punctation like bang instead.
In command s
and t
, just type a different
character after their name is enough to specify the delimiter.
But in text matching, you must prepend a backslash to the specified delimiter so that it can be idenified as the different delimiter you want.