New in 3.5.0: The AdminServer is an embedded Jetty server that provides an HTTP interface to the four letter word commands. By default, the server is started on port 8080, and commands are issued by going to the URL "/commands/[command name]", e.g., http://localhost:8080/commands/stat. The command response is returned as JSON. Unlike the original protocol, commands are not restricted to four-letter names, and commands can have multiple names; for instance, "stmk" can also be referred to as "set_trace_mask". To view a list of all available commands, point a browser to the URL /commands (e.g., http://localhost:8080/commands). See the AdminServer configuration options for how to change the port and URLs.
# 在每一行中查找字串“foo”,并将找到的“foo”替换为“bar” sed 's/foo/bar/'# 只替换每一行中的第一个“foo”字串 sed 's/foo/bar/4'# 只替换每一行中的第四个“foo”字串 sed 's/foo/bar/g'# 将每一行中的所有“foo”都换成“bar” sed 's/\(.*\)foo\(.*foo\)/\1bar\2/'# 替换倒数第二个“foo” sed 's/\(.*\)foo/\1bar/'# 替换最后一个“foo”
# 只在行中出现字串“baz”的情况下将“foo”替换成“bar” sed '/baz/s/foo/bar/g'
# 将“foo”替换成“bar”,并且只在行中未出现字串“baz”的情况下替换 sed '/baz/!s/foo/bar/g'
# 不管是 “scarlet”“ruby”还是“puce”,一律换成 “red” sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'#对多数的sed都有效 gsed 's/scarlet\|ruby\|puce/red/g'# 只对GNU sed有效
# 倒置所有行,第一行成为最后一行,依次类推(模拟“tac”)。 # 由于某些原因,使用下面命令时HHsed v1.5会将文件中的空行删除 sed '1!G;h;$!d'# 方法1 sed -n '1!G;h;$p'# 方法2
# 将行中的字符逆序排列,第一个字成为最后一字,……(模拟“rev”) sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
# 将每两行连接成一行(类似“paste”) sed '$!N;s/\n/ /'
# 如果当前行以反斜杠“\”结束,则将下一行并到当前行末尾 # 并去掉原来行尾的反斜杠 sed -e :a -e '/\\$/N; s/\\\n//; ta'
# 显示文件中的最后 10 行 (模拟“tail”) sed -e :a -e '$q;N;11,$D;ba'
# 显示文件中的最后2行(模拟“tail -2”命令) sed '$!N;$!D'
# 显示文件中的最后一行(模拟“tail -1”) sed '$!d'# 方法1 sed -n '$p'# 方法2
# 显示文件中的倒数第二行 sed -e '$!{h;d;}' -e x # 当文件中只有一行时,输入空行 sed -e '1{$q;}' -e '$!{h;d;}' -e x # 当文件中只有一行时,显示该行 sed -e '1{$d;}' -e '$!{h;d;}' -e x # 当文件中只有一行时,不输出
# 只显示匹配正则表达式的行(模拟“grep”) sed -n '/regexp/p'# 方法1 sed '/regexp/!d'# 方法2
# 只显示“不”匹配正则表达式的行(模拟“grep -v”) sed -n '/regexp/!p'# 方法1,与前面的命令相对应 sed '/regexp/d'# 方法2,类似的语法
# 查找“regexp”并将匹配行的上一行显示出来,但并不显示匹配行 sed -n '/regexp/{g;1!p;};h'
# 查找“regexp”并将匹配行的下一行显示出来,但并不显示匹配行 sed -n '/regexp/{n;p;}'
# 显示包含“regexp”的行及其前后行,并在第一行之前加上“regexp”所 # 在行的行号 (类似“grep -A1 -B1”) sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
# 显示包含“AAA”、“BBB”或“CCC”的行(任意次序) sed '/AAA/!d; /BBB/!d; /CCC/!d'# 字串的次序不影响结果
# 显示包含“AAA”、“BBB”和“CCC”的行(固定次序) sed '/AAA.*BBB.*CCC/!d'
# 显示包含“AAA”“BBB”或“CCC”的行 (模拟“egrep”) sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d # 多数sed gsed '/AAA\|BBB\|CCC/!d'# 对GNU sed有效
括号语法:前面的例子对 sed 命令基本上都使用单引号(‘…’)而非双引号(“…”)这是因为sed通常是在Unix平台上使用。单引号下,Unix 的 shell(命令解释器)不会对美元符($)和后引号(...)进行解释和执行。而在双引号下美元符会被展开为变量或参数的值,后引号中的命令被执行并以输出的结果代替后引号中的内容。而在“csh”及其衍生的shell中使用感叹号(!)时需要在其前面加上转义用的反斜杠(就像这样:!)以保证上面所使用的例子能正常运行(包括使用单引号的情况下)。DOS版本的Sed则一律使用双引号(“…”)而不是引号来圈起命令。
‘\t’的用法:为了使本文保持行文简洁,我们在脚本中使用’\t’来表示一个制表符。但是现在大部分版本的sed还不能识别’\t’的简写方式,因此当在命令行中为脚本输入制表符时,你应该直接按TAB键来输入制表符而不是输入’\t’。下列的工具软件都支持’\t’做为一个正则表达式的字元来表示制表符:awk、perl、HHsed、sedmod以及GNU sed v3.02.80。
Al Aab # 建立了“seders”邮件列表
Edgar Allen # 许多方面
Yiorgos Adamopoulos # 许多方面
Dale Dougherty # 《sed & awk》作者
Carlos Duarte # 《do it with sed》作者
Eric Pement # 本文档的作者
Ken Pizzini # GNU sed v3.02 的作者
S.G. Ravenhall # 去html标签脚本
Greg Ubben # 有诸多贡献并提供了许多帮助