3. ★& 代 表 最 后 匹 配
★利 用 sed 修 改 PATH 环 境 变 量
★测 试 并 提 高 sed 命 令 运 行 效 率
★指 定 输 出 文 件 [address1][,address2]w outputfile
★指 定 输 入 文 件 [address]r inputfile
★替 换 相 应 字 符 [address1][,address2]y/old/new/
★! 号 的 使 用
★ c 正 则 表 达 式 c的 使 用
★ sed 命 令 中 正 则 表 达 式 的 复 杂 性
★转 换 man 手 册 成 普 通 文 本 格 式 ( 新 )
★ sed 的 man 手 册 ( 用 的 就 是 上 面 的 方 法 )
★命 令 行 参 数 简 介
sed
-e script 指 定 sed 编 辑 命 令
-f scriptfile 指 定 的 文 件 中 是 sed 编 辑 命 令
-n 寂 静 模 式 , 抑 制 来 自 sed 命 令 执 行 过 程 中 的 冗 余 输 出 信 息 , 比 如 只
显 示 那 些 被 改 变 的 行 。
不 明 白 ? 不 要 紧 , 把 这 些 肮 脏 丢 到 一 边 , 跟 我 往 下 走 , 不 过 下 面 的 介 绍 里
不 包 括 正 则 表 达 式 的 解 释 , 如 果 你 不 明 白 , 可 能 有 点 麻 烦 。
★首 先 假 设 我 们 有 这 样 一 个 文 本 文 件 sedtest.txt
cat > sedtest.txt
Sed is a stream editor
----------------------
A stream editor is used to perform basic text transformations on an input stream
--------------------------------------------------------------------------------
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
sed works by making only one pass over the input(s), and is consequently more
-----------------------------------------------------------------------------
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
--------------------------------------------------------------------------------
-
★输 出 指 定 范 围 的 行 p other types of editors.
sed -e "1,4p" -n sedtest.txt
sed -e "/from/p" -n sedtest.txt
sed -e "1,/from/p" -n sedtest.txt
★在 每 一 行 前 面 增 加 一 个 制 表 符 (^I)
4. sed "s/^/^I/g" sedtest.txt
注 意 ^I 的 输 入 方 法 是 ctrl-v ctrl-i
单 个 ^ 表 示 行 首
★在 每 一 行 后 面 增 加 --end
sed "s/$/--end/g" sedtest.txt
单 个 $ 表 示 行 尾
★显 示 指 定 模 式 匹 配 行 的 行 号 [/pattern/]=
sed -e '/is/=' sedtest.txt
1
Sed is a stream editor
----------------------
3
A stream editor is used to perform basic text transformations on an input stream
--------------------------------------------------------------------------------
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
7
sed works by making only one pass over the input(s), and is consequently more
-----------------------------------------------------------------------------
9
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
--------------------------------------------------------------------------------
-
-
意 思 是 分 析 sedtest.txt , 显 示 那 些 包 含 is 串 的 匹 配 行 的 行 号 , 注 意 11 行 中 出 现 了 is 字 符 串
这 个 输 出 是 面 向 stdout 的 , 如 果 不 做 重 定 向 处 理 , 则 不 影 响 原 来 的 sedtest.txt
★在 匹 配 行 后 面 增 加 文 本 [/pattern/]a 或 者 [address]a
^D
sed -f sedadd.script sedtest.txt
Sed is a stream editor
A stream editor is used to perform basic text transformations on an input stream
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
sed works by making only one pass over the input(s), and is consequently more
5. -----------------------------------------------------------------------------
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
--------------------------------------------------------------------------------
-
-
[scz@ /home/scz/src]> sed -e "a
+++++++++
---------------------------------------------
找 到 包 含 from 字 符 串 的 行 , 在 该 行 的 下 一 行 增 加 +++++++++ 。
这 个 输 出 是 面 向 stdout 的 , 如 果 不 做 重 定 向 处 理 , 则 不 影 响 原 来 的 sedtest.txt
很多人想在命令行上直接完成这个操作而不是多一个 sedadd.script,不幸的是,这需要用?nbsp;
?nbsp;
续 行 符 ,
[scz@ /home/scz/src]> sed -e "/from/a
> +++++++++" sedtest.txt
[scz@ /home/scz/src]> sed -e "a
> +++++++++" sedtest.txt
上 面 这 条 命 令 将 在 所 有 行 后 增 加 一 个 新 行 +++++++++
[scz@ /home/scz/src]> sed -e "1 a
> +++++++++" sedtest.txt
把 下 面 这 两 行 copy/paste 到 一 个 shell 命 令 行 上 , 效 果 一 样
+++++++++" sedtest.txt
[address]a 只 接 受 一 个 地 址 指 定
对于 a 命令,不支持单引号,只能用双引号,而对于 d 命令等其他命令,同时
★删 除 匹 配 行 [/pattern/]d 或 者 [address1][,address2]d
sed -e '/---------------------------------------------/d' sedtest.txt
Sed is a stream editor
A stream editor is used to perform basic text transformations on an input stream
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
sed works by making only one pass over the input(s), and is consequently more
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
sed -e '6,10d' sedtest.txt
删 除 6-10 行 的 内 容 , 包 括 6 和 10
sed -e "2d" sedtest.txt
删 除 第 2 行 的 内 容
sed "1,/^$/d" sedtest.txt
删 除 从 第 一 行 到 第 一 个 空 行 之 间 的 所 有 内 容
注 意 这 个 命 令 很 容 易 带 来 意 外 的 结 果 , 当 sedtest.txt 中 从 第 一 行 开 始 并 没 有 空 行 , 则 sed 删
6. ?nbsp;
?nbsp;
sed "1,/from/d" sedtest.txt
删 除 从 第 一 行 到 第 一 个 包 含 from 字 符 串 的 行 之 间 的 所 有 内 容 , 包 括 第 一 个 包 含
from 字 符 串 的 行 。
★替 换 匹 配 行 [/pattern/]c 或 者 [address1][,address2]c
sed -e "/is/c
**********" sedtest.txt
寻 找 所 有 包 含 is 字 符 串 的 匹 配 行 , 替 换 成 **********
**********
----------------------
**********
--------------------------------------------------------------------------------
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
**********
-----------------------------------------------------------------------------
**********
--------------------------------------------------------------------------------
-
sed -e "1,11c
**********" sedtest.txt----------------------
在 1-12 行 内 搜 索 所 有 from 字 符 串 , 分 别 替 换 成 **** 字 符 串
★限 定 范 围 后 的 模 式 匹 配
sed "/But/s/is/are/g" sedtest.txt
对 那 些 包 含 But 字 符 串 的 行 , 把 is 替 换 成 are
sed "/is/s/t/T/" sedtest.txt
对 那 些 包 含 is 字 符 串 的 行 , 把 每 行 第 一 个 出 现 的 t 替 换 成 T
sed "/While/,/from/p" sedtest.txt -n
输 出 在 这 两 个 模 式 匹 配 行 之 间 的 所 有 内 容
★指 定 替 换 每 一 行 中 匹 配 的 第 几 次 出 现
sed "s/is/are/5" sedtest.txt
把 每 行 的 is 字 符 串 的 第 5 次 出 现 替 换 成 are
★& 代 表 最 后 匹 配
sed "s/^$/(&)/" sedtest.txt
给 所 有 空 行 增 加 一 对 ()
sed "s/is/(&)/g" sedtest.txt
给 所 有 is 字 符 串 外 增 加 ()
sed "s/.*/(&)/" sedtest.txt
给 所 有 行 增 加 一 对 ()
8. cat > sedtest.txt
^/[}]{.*}[(]$)
^D
如 何 才 能 把 该 行 替 换 成
(]$)/[}]{.*}^[
★转 换 man 手 册 成 普 通 文 本 格 式 ( 新 )
man sed | col -b > sed.txt
sed -e "s/^H//g" -e "/^$/d" -e "s/^^I/ /g" -e "s/^I/ /g" sed.txt > sedman
txt
删除所有退格键、空行,把行首的制表符替换成 8 个空格,其余制表符替换成一个空格。
★ sed 的 man 手 册 ( 用 的 就 是 上 面 的 方 法 )
NAME
sed - a Stream EDitor
SYNOPSIS
sed [-n] [-V] [--quiet] [--silent] [--version] [--help]
[-e script] [--expression=script]
[-f script-file] [--file=script-file]
[script-if-no-other-script]
[file...]
DEscriptION
Sed is a stream editor. A stream editor is used to per-
form basic text transformations on an input stream (a file
or input from a pipeline). While in some ways similar to
an editor which permits scripted edits (such as ed), sed
works by making only one pass over the input(s), and is
consequently more efficient. But it is sed's ability to
filter text in a pipeline which particularly distinguishes
it from other types of editors.
OPTIONS
Sed may be invoked with the following command-line
options:
-V
--version
Print out the version of sed that is being run and
a copyright notice, then exit.
-h
--help Print a usage message briefly summarizing these
command-line options and the bug-reporting address,
then exit.
-n
--quiet
--silent
By default, sed will print out the pattern space at
the end of each cycle through the script. These
9. options disable this automatic printing, and sed
will only produce output when explicitly told to
via the p command.
-e script
--expression=script
Add the commands in script to the set of commands
to be run while processing the input.
-f script-file
--file=script-file
Add the commands contained in the file script-file
to the set of commands to be run while processing
the input.
If no -e,-f,--expression, or --file options are given on
the command-line, then the first non-option argument on
the command line is taken to be the script to be executed.
If any command-line parameters remain after processing the
above, these parameters are interpreted as the names of
input files to be processed. A file name of - refers to
the standard input stream. The standard input will pro-
cessed if no file names are specified.
Command Synopsis
This is just a brief synopsis of sed commands to serve as
a reminder to those who already know sed; other documenta-
tion (such as the texinfo document) must be consulted for
fuller descriptions.
Zero-address ``commands''
: label
Label for b and t commands.
#comment
The comment extends until the next newline (or the
end of a -e script fragment).
} The closing bracket of a { } block.
Zero- or One- address commands
= Print the current line number.
a
text Append text, which has each embedded newline pre-
ceeded by a backslash.
i
text Insert text, which has each embedded newline pre-
ceeded by a backslash.
q Immediately quit the sed script without processing
any more input, except that if auto-print is not
diabled the current pattern space will be printed.
r filename
10. Append text read from filename.
Commands which accept address ranges
{ Begin a block of commands (end with a }).
b label
Branch to label; if label is omitted, branch to end
of script.
t label
If a s/// has done a successful substitution since
the last input line was read and since the last t
command, then branch to label; if label is omitted,
branch to end of script.
c
text Replace the selected lines with text, which has
each embedded newline preceeded by a backslash.
d Delete pattern space. Start next cycle.
D Delete up to the first embedded newline in the pat-
tern space. Start next cycle, but skip reading
from the input if there is still data in the pat-
tern space.
h H Copy/append pattern space to hold space.
g G Copy/append hold space to pattern space.
x Exchange the contents of the hold and pattern
spaces.
l List out the current line in a ``visually unambigu-
ous'' form.
n N Read/append the next line of input into the pattern
space.
p Print the current pattern space.
P Print up to the first embedded newline of the cur-
rent pattern space.
s/regexp/replacement/
Attempt to match regexp against the pattern space.
If successful, replace that portion matched with
replacement. The replacement may contain the spe-
cial character & to refer to that portion of the
pattern space which matched, and the special
escapes 1 through 9 to refer to the corresponding
matching sub-expressions in the regexp.
w filename Write the current pattern space to file-
name.
y/source/dest/
Transliterate the characters in the pattern space
which appear in source to the corresponding charac-
ter in dest.
11. Addresses
Sed commands can be given with no addresses, in which case
the command will be executed for all input lines; with one
address, in which case the command will only be executed
for input lines which match that address; or with two
addresses, in which case the command will be executed for
all input lines which match the inclusive range of lines
starting from the first address and continuing to the sec-
ond address. Three things to note about address ranges:
the syntax is addr1,addr2 (i.e., the addresses are sepa-
rated by a comma); the line which addr1 matched will
always be accepted, even if addr2 selects an earlier line;
and if addr2 is a regexp, it will not be tested against
the line that addr1 matched.
After the address (or address-range), and before the com-
mand, a ! may be inserted, which specifies that the com-
mand shall only be executed if the address (or address-
range) does not match.
The following address types are supported:
number Match only the specified line number.
first~step
Match every step'th line starting with line first.
For example, ``sed -n 1~2p'' will print all the
odd-numbered lines in the input stream, and the
address 2~5 will match every fifth line, starting
with the second. (This is a GNU extension.)
$ Match the last line.
/regexp/
Match lines matching the regular expression regexp.
cregexpc
Match lines matching the regular expression regexp.
The c may be any character.
Regular expressions
POSIX.2 BREs should be supported, but they aren't com-
pletely yet. The n sequence in a regular expression
matches the newline character. There are also some GNU
extensions. [XXX FIXME: more needs to be said. At the
very least, a reference to another document which
describes what is supported should be given.]
Miscellaneous notes
This version of sed supports a <newline> sequence in all
regular expressions, the replacement part of a substitute
(s) command, and in the source and dest parts of a
transliterate (y) command. The is stripped, and the
12. newline is kept.
SEE ALSO
awk(1), ed(1), expr(1), emacs(1), perl(1), tr(1), vi(1),
regex(5) [well, one ought to be written... XXX], sed.info,
any of various books on sed, the sed FAQ
( http://www.wollery.demon.co.uk/sedtut10.txt,
http://www.ptug.org/sed/sedfaq.htm).
BUGS
E-mail bug reports to bug-gnu-utils@gnu.org. Be sure to
include the word ``sed'' somewhere in the ``Subject:''
field.
Sed 学 习 笔 记
--------------------------------------------------------------------------------
Table of Contents
1. Sed 简 介
2. 定 址
3. Sed 命 令
4. 选 项
5. 元 字 符 集
6. 实 例
7. 脚 本
1. Sed 简 介
sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模
式空间”(pattern space),接着用 sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往
屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。
Sed 主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。以下介绍的是 Gnu 版
本 的 Sed 3.02 。
2. 定 址
可以通过定址来定位你所希望编辑的行,该地址用数字构成,用逗号分隔的两个行数表示以这两行为起止