HANDY ONE-LINERS FOR SED——SED一句话手册

HANDY ONE-LINERS FOR SED (Unix stream editor)               Feb. 12, 2003

compiled by Eric Pement <pemente@northpark.edu>               version 5.2

Latest version of this file is usually at:

   http://www.student.northpark.edu/pemente/sed/sed1line.txt

   http://www.cornerstonemag.com/sed/sed1line.txt

This file is also available in Portuguese at:

   http://www.lrv.ufsc.br/wmaker/sed_ptBR.html

 

FILE SPACING:

文件空行

# double space a file

# 使文件原来每一行变成两行,新加的一行用空行补充

sed G

 

# double space a file which already has blank lines in it. Output file

# should contain no more than one blank line between lines of text.

# 文件中有双空行的现象, 输出文件 两行之间仅且只有单空行

sed '/^$/d;G'

 

# triple space a file

# 使文件原来每一行变成三行,新加的两行用空行补充

sed 'G;G'

 

# undo double-spacing (assumes even-numbered lines are always blank)

# 撤消 sed G 形成的结果(假定偶数行总是有空行)

sed 'n;d'

 

# insert a blank line above every line which matches "regex"

# 所有 匹配到表达式的上一行插入空行

sed '/regex/{x;p;x;}'

 

# insert a blank line below every line which matches "regex"

# 所有 匹配到表达式的下一行插入空行

sed '/regex/G'

 

# insert a blank line above and below every line which matches "regex"

# 在所有匹配到表达式的上下都插入空行

sed '/regex/{x;p;x;G;}'

 

理解:对于输入的文件,以行读取,假定第一行就匹配到,则模式空间中已经存在第一行,此时第一个 x 将模式空间和保留空间(保留空间内容为空行)内容互换, p 打印出模式空间的内容,所以先出现一个空行,再执行 x ,又进行互换,则模式空间又有了第一行内容,此时执行 G, 将保留空间追加到模式空间,所以再输出到屏幕就是第一行和一个空行。所以最后会把每一行的上下都增加一个空行。

 

NUMBERING:

编号

# number each line of a file (simple left alignment). Using a tab (see

# note on '/t' at end of file) instead of space will preserve margins.

# 给文件每一行编号(左对齐)。用 TAB 键(见文件最后注释中 ”/t” )替换每一行的回车。

sed = filename | sed 'N;s//n//t/'

 

理解:执行 = 后,文件每一行被编号,但编号后有回车。将编号完成的内容再进行处理,首先遇到 N ,此命令把现在读取到模式空间后的一行追加到模式空间,再执行替换,把回车去除,所以编号与原文件中的每一行都在同一行了。

 

# number each line of a file (number on left, right-aligned)

给文件的每一行编号(编号靠左,整体右对齐)

sed = filename | sed 'N; s/^/     /; s/ */(./{6,/}/)/n//1  /'

不知道是否是想写成这样:

sed = filename | sed 'N; s/^/     /; s//n//t/;s/ */(./{6,/}/)/n//1  /'

 

 

# number each line of file, but only print numbers if line is not blank

# 为文件中除空行外的每一行编号(但空行也占用了编号)

sed '/./=' filename | sed '/./N; s//n/ /'

 

# count lines (emulates "wc -l")

# 对行计数(模拟“ wc –l ”功能)

sed -n '$='

 

TEXT CONVERSION AND SUBSTITUTION:

文本转换和和替换

# IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format

# UNIX 环境中:将 DOS 系统的换行( CR/LF )转换为 Unix 格式

sed 's/.$//'               # assumes that all lines end with CR/LF

sed 's/^M$//'              # in bash/tcsh, press Ctrl-V then Ctrl-M

sed 's//x0D$//'            # gsed 3.02.80, but top script is easier

                                                

# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format

# UNIX 环境中:将 Unix 系统的换行( LF )转换为 DOS 格式

 

sed "s/$/`echo -e ///r`/"            # command line under ksh

sed 's/$'"/`echo ///r`/"             # command line under bash

sed "s/$/`echo ///r`/"               # command line under zsh

sed 's/$//r/'                        # gsed 3.02.80

 

# IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format

# DOS 环境中:将 Unix 系统的换行( LF )转换为 DOS 格式

 

sed "s/$//"                          # method 1

sed -n p                             # method 2

                                                       

 

# IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format

# Cannot be done with DOS versions of sed. Use "tr" instead.

# DOS 环境中:将 DOS 系统的换行( CR/LF )转换为 Unix 格式不能用 DOS 版本的 sed 处理。用 tr 代替

tr -d /r <infile >outfile            # GNU tr version 1.22 or higher

 

# delete leading whitespace (spaces, tabs) from front of each line

# aligns all text flush left

# 删除每一行开头的空格或 tab 键使每一行左对齐

 

sed 's/^[ /t]*//'                    # see note on '/t' at end of file

 

# delete trailing whitespace (spaces, tabs) from end of each line

# 删除每一行尾部的空格或 tab

sed 's/[ /t]*$//'                    # see note on '/t' at end of file

 

# delete BOTH leading and trailing whitespace from each line

# 删除每一行开头和结尾的空格或 tab

sed 's/^[ /t]*//;s/[ /t]*$//'

 

# insert 5 blank spaces at beginning of each line (make page offset)

# 在每一行开头插入 5 个空格(做页面偏移)

sed 's/^/     /'

 

# align all text flush right on a 79-column width

# 使所有文本以 79 列宽度右对齐

sed -e :a -e 's/^./{1,78/}$/ &/;ta'  # set at 78 plus 1 space

 

# center all text in the middle of 79-column width. In method 1,

# spaces at the beginning of the line are significant, and trailing

# spaces are appended at the end of the line. In method 2, spaces at

# the beginning of the line are discarded in centering the line, and

# no trailing spaces appear at the end of lines.

 

# 使所有文本以 79 列宽度中间对齐。在方法 1 中,空格在每一行开头很重要,在尾部的空格是追加在每一行的末尾的。方法 2 ,在每一行要剧中的行的开头部分增加无用的空格,在行尾不需求出现空格

 

 

 

sed  -e :a -e 's/^./{1,77/}$/ & /;ta'                     # method 1

sed  -e :a -e 's/^./{1,77/}$/ &/;ta' -e 's//( */)/1//1/'  # method 2

 

# substitute (find and replace) "foo" with "bar" on each line

# 在每一行中用 bar 替换 foo

 

sed 's/foo/bar/'             # replaces only 1st instance in a line

sed 's/foo/bar/4'            # replaces only 4th instance in a line

sed 's/foo/bar/g'            # replaces ALL instances in a line

sed 's//(.*/)foo/(.*foo/)//1bar/2/' # replace the next-to-last case

sed 's//(.*/)foo//1bar/'            # replace only the last case

 

# substitute "foo" with "bar" ONLY for lines which contain "baz"

# 在匹配到 baz 的那一行把 bar 替换为 foo

sed '/baz/s/foo/bar/g'

 

# substitute "foo" with "bar" EXCEPT for lines which contain "baz"

# 在匹配到 baz 的那一行不把 bar 替换为 foo

sed '/baz/!s/foo/bar/g'

 

# change "scarlet" or "ruby" or "puce" to "red"

# 把每一行中的 scarlet ruby puce 替换为 red

sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'   # most seds

gsed 's/scarlet/|ruby/|puce/red/g'                # GNU sed only

 

# reverse order of lines (emulates "tac")

# 反转每一行的顺序(模拟 tac

# bug/feature in HHsed v1.5 causes blank lines to be deleted

#bug/ feature 1.5 版本中造成空行被删除

sed '1!G;h;$!d'               # method 1

sed -n '1!G;h;$p'             # method 2

 

理解( method 1 ):首先对第一行进行处理,在模式空间中的第一行不增加 保留空间中的空行,执行 h ,将模式空间中的内容复制 到保留空间,保留空间不再为空,执行 $!d, 此时模式空间中内容为非最后一行则其中内容将被删除,进行下一个循环,现在模式空间内容为空,保留空间内容为第一行内容。看第二行,首先第二行后要增加保留空间中的内容,则模式空间中的内容分别是第二行第一行,再执行 h ,保留空间内容再次变化,存储了第二行第一行,再执行 $!d ,如果是最后一行就停止了,不删除任何东西。所以就成功的反转了这两行。如果还有第三行,则这个删除动作还要执行,再进行第三次循环,模式空间中先有第三行,再将保留空间中的第二行第一行增加进去,再复制模式空间到保留空间,是最后一行,不删除,反转成功。

 

# reverse each character on the line (emulates "rev")

# 反转字符串(模拟 rev

sed '//n/!G;s//(./)/(.*/n/)/&/2/1/;//D;s/.//'

理解:对 123 进行反转。首先看是否匹配到 /n 没有匹配到则增加保留空间内容到模式空间, 123 后没有回车,所以增加了一个空行。再执行替换,这个复杂,先匹配单个字符做为标签 1 ,再匹配标签 1 后的所有为标签 2 ,这标签 1 和标签 2 就是 & ,包括了 1 23 回车空行两部分,进行替换, 1 23 回车空行先保留,再增加上 23 回车空行,再增加上 1 则,不执行后面的结果就是:

123

23

1

或者说模式空间中的内容应该是 123/n23/n1

那再执行后面的命令, //D 这个命令比较特殊, // 表示最近使用的 pattern ,所以 //D 就是在模式空间删除第一行 , 执行完成后如果模式空间不为空,继续下一个循环, 所以直接进行删除模式空间中的内容直到第一个 /n ,那就只剩下 23/n1 ,此时再循环,从命令开头进行,此时不追加空行,因为有 /n ,所以直接进行再次替换, 2 3/n 又是匹配到了两部分,替换后成为 23/n3/n2 ,别忘记了还有一个 1 呢,它是在 /n 之后,所以要原样保留的,所以最后就是 23/n3/n21 ;现在再进行一次删除,剩下 3/n21 ,再进行替换,匹配到 3 /n ,替换完成是 3/n/n3 再加上 21 就是 3/n/n321 此时再进行一次删除, 成为 /n321 ,现在在模式空间中第一行就是一个换行,所以 D 的循环停止,执行后面的 s/.// 替换,将第一个字符换为空,所以 /n 也没有了,成为 321 ,反转成功。这里难以理解就是 //D

 

# join pairs of lines side-by-side (like "paste")

# 把两行组合在一起

sed '$!N;s//n/ /'

 

# if a line ends with a backslash, append the next line to it

# 如果是以反斜杆结尾,则将它的下一行追加到它后面

sed -e :a -e '///$/N; sn//; ta'

 

# if a line begins with an equal sign, append it to the previous line

# and replace the "=" with a single space

# 如果是以 = 开头,则将它追加到前面的那一行并用一个空格替换此“ =

 

sed -e :a -e '$!N;s//n=/ /;ta' -e 'P;D'

 

# add commas to numeric strings, changing "1234567" to "1,234,567"

# 增加逗号到字数串中(每 3 位一组),例如将 1234567 变为 1,234,567

 

gsed ':a;s//B[0-9]/{3/}/>/,&/;ta'                     # GNU sed

sed -e :a -e 's//(.*[0-9]/)/([0-9]/{3/}/)//1,/2/;ta'  # other seds

 

# add commas to numbers with decimal points and minus signs (GNU sed)

# 增加逗号到有小数点的和负号的数字串中

 

gsed ':a;s//(^/|[^0-9.]/)/([0-9]/+/)/([0-9]/{3/}/)//1/2,/3/g;ta'

 

# add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)

# 每五行增加一个空格

 

gsed '0~5G'                  # GNU sed only

sed 'n;n;n;n;G;'             # other seds

 

SELECTIVE PRINTING OF CERTAIN LINES:

选择性输出指定行

# print first 10 lines of file (emulates behavior of "head")

# 输出前 10 行(模拟 head 命令行为)

sed 10q

 

# print first line of file (emulates "head -1")

# 输出第一行(模拟 head -1 命令行为)

sed q

 

# print the last 10 lines of a file (emulates "tail")

# 输出最后 10 行(模拟 tail 命令)

sed -e :a -e '$q;N;11,$D;ba'

 

# print the last 2 lines of a file (emulates "tail -2")

# 输出最后 2 行(模拟 tail -2 命令)

sed '$!N;$!D'

 

# print the last line of a file (emulates "tail -1")

# 输出最后一行(模拟 tail -1 命令)

sed '$!d'                    # method 1

sed -n '$p'                  # method 2

 

# print only lines which match regular expression (emulates "grep")

# 输出匹配到表达式的行(模拟 grep

sed -n '/regexp/p'           # method 1

sed '/regexp/!d'             # method 2

 

# print only lines which do NOT match regexp (emulates "grep -v")

# 输出没有匹配到表达式的行(模拟 grep -v

sed -n '/regexp/!p'          # method 1, corresponds to above

sed '/regexp/d'              # method 2, simpler syntax

 

# print the line immediately before a regexp, but not the line

# containing the regexp

# 输出匹配行的前一行,这一行并不包含表达式

 

sed -n '/regexp/{g;1!p;};h'

 

# print the line immediately after a regexp, but not the line

# 输出匹配行的后一行,

# containing the regexp

# 这一行并不包含表达式

sed -n '/regexp/{n;p;}'

 

# print 1 line of context before and after regexp, with line number

# indicating where the regexp occurred (similar to "grep -A1 -B1")

# 输出匹配行的前后两行,用行号指出表达式匹配到了(类似于 grep –A1 –B1

 

sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

 

# grep for AAA and BBB and CCC (in any order)

# 查询 AAA BBB CCC (以任何顺序排列)

 

sed '/AAA/!d; /BBB/!d; /CCC/!d'

 

# grep for AAA and BBB and CCC (in that order)

# 查询 AAA BBB CCC (以指定顺序排列)

 

sed '/AAA.*BBB.*CCC/!d'

 

# grep for AAA or BBB or CCC (emulates "egrep")

sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d    # most seds

gsed '/AAA/|BBB/|CCC/!d'                        # GNU sed only

 

# print paragraph if it contains AAA (blank lines separate paragraphs)

# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below

# 输出图形如果包含 AAA

 

 

sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'

 

# print paragraph if it contains AAA and BBB and CCC (in any order)

sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'

 

# print paragraph if it contains AAA or BBB or CCC

sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d

gsed '/./{H;$!d;};x;/AAA/|BBB/|CCC/b;d'         # GNU sed only

 

# print only lines of 65 characters or longer

# 输出刚好 65 个字符的行

sed -n '/^./{65/}/p'

 

# print only lines of less than 65 characters

# 输出字符宽度比 65 短的行

sed -n '/^./{65/}/!p'        # method 1, corresponds to above

sed '/^./{65/}/d'            # method 2, simpler syntax

 

# print section of file from regular expression to end of file

# 打印文件末尾且匹配到的表达式的行

sed -n '/regexp/,$p'

 

# print section of file based on line numbers (lines 8-12, inclusive)

# 打印打印指定的行

sed -n '8,12p'               # method 1

sed '8,12!d'                 # method 2

 

# print line number 52

# 打印第 52

sed -n '52p'                 # method 1

sed '52!d'                   # method 2

sed '52q;d'                  # method 3, efficient on large files

 

# beginning at line 3, print every 7th line

# 打印第三到第 7

gsed -n '3~7p'               # GNU sed only

sed -n '3,${p;n;n;n;n;n;n;}' # other seds

 

# print section of file between two regular expressions (inclusive)

# 打印匹配到两个表达式之间的行

sed -n '/Iowa/,/Montana/p'             # case sensitive

 

SELECTIVE DELETION OF CERTAIN LINES:

选择性的删除匹配到的行

 

# print all of file EXCEPT section between 2 regular expressions

# 打印除匹配到两个表达间的行

sed '/Iowa/,/Montana/d'

 

# delete duplicate, consecutive lines from a file (emulates "uniq").

# First line in a set of duplicate lines is kept, rest are deleted.

# 删除重复的,连续的行(模拟 uniq )保留在很多重复行中的第一行,其他的删除

 

sed '$!N; /^/(.*/)/n/1$/!P; D'

理解:读入第一行,将第二行增加到模式空间,假定第一和第二行内容一样;执行匹配,匹配开头的第一行,将第一行 /n 前的部分当作标签再接着匹配到整个模式空间结束,非匹配到就直接打印,现在两行内容一样说明已经匹配到,所以不能打印;执行删除,将第一行删除,还剩下第二行,如果只两行,则结束。如果还有第三行,也和前两行一样,刚好是最后一行,则空间中有第二和第三个,两行一样能匹配到,不打印,删除第二行,留下第三行,结束。另外这里的第一行需要再进一步确认,模式空间是否是以栈的形式进行装载数据,那第一行就是栈顶( top )。

 

 

# delete duplicate, nonconsecutive lines from a file. Beware not to

# overflow the buffer size of the hold space, or else use GNU sed.

# 删除重复的、不连续的行。注意不要溢出保留空间的缓存大小,否则请使用 GNU sed

 

sed -n 'G; s//n/&&/; /^/([ -~]*/n/).*/n/1/d; s//n//; h; P'

 

# delete the first 10 lines of a file

# 删除前 10

sed '1,10d'

 

# delete the last line of a file

# 删除最后一行

sed '$d'

 

# delete the last 2 lines of a file

# 删除最后两行

sed 'N;$!P;$!D;$d'

 

# delete the last 10 lines of a file

# 删除最后 10

sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1

sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2

 

# delete every 8th line

# 每隔 8 行删除一行

gsed '0~8d'                           # GNU sed only

sed 'n;n;n;n;n;n;n;d;'                # other seds

 

# delete ALL blank lines from a file (same as "grep '.' ")

# 删除所有的空行

sed '/^$/d'                           # method 1

sed '/./!d'                           # method 2

 

# delete all CONSECUTIVE blank lines from file except the first; also

# deletes all blank lines from top and end of file (emulates "cat -s")

# 删除所有的连续空行除了第一个空行同时也删除所有的空行(模拟 cat -s

sed '/./,/^$/!d'          # method 1, allows 0 blanks at top, 1 at EOF

sed '/^$/N;//n$/D'        # method 2, allows 1 blank at top, 0 at EOF

 

# delete all CONSECUTIVE blank lines from file except the first 2:

# 删除所有的连续空行,除了第二行

sed '/^$/N;//n$/N;//D'

 

# delete all leading blank lines at top of file

# 删除了所有开头的空行

sed '/./,$!d'

 

# delete all trailing blank lines at end of file

# 删除所有末尾的空行

sed -e :a -e '/^/n*$/{$d;N;ba' -e '}'  # works on all seds

sed -e :a -e '/^/n*$/N;//n$/ba'        # ditto, except for gsed 3.02*

 

# delete the last line of each paragraph

# 删除每个段落的最后一行

sed -n '/^$/{p;h;};/./{x;/./p;}'

 

SPECIAL APPLICATIONS:

特殊的应用

# remove nroff overstrikes (char, backspace) from man pages. The 'echo'

# command may need an -e switch if you use Unix System V or bash shell.

# 去除这个非打印字符。如果你在 Unix V 系统或是 bash shell 使用 Echo 命令需要使用 -e 选项

 

sed "s/.`echo ///b`//g"    # double quotes required for Unix environment

sed 's/.^H//g'             # in bash/tcsh, press Ctrl-V and then Ctrl-H

sed 's/./x08//g'           # hex expression for sed v1.5

 

# get Usenet/e-mail message header

# 得到 新闻网 / 电邮头

sed '/^$/q'                # deletes everything after first blank line

 

# get Usenet/e-mail message body

# 得到 新闻网 / 电邮本体

sed '1,/^$/d'              # deletes everything up to first blank line

 

# get Subject header, but remove initial "Subject: " portion

# 得到主题,但不包含“ Subject: ”部分

sed '/^Subject: */!d; s///;q'

 

# get return address header

# 得到回信地址

sed '/^Reply-To:/q; /^From:/h; /./d;g;q'

 

# parse out the address proper. Pulls out the e-mail address by itself

# 解析出合适的地址。抽取出自身的邮件地址从返回的一行地址

# from the 1-line return address header (see preceding script)

sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'

 

# add a leading angle bracket and space to each line (quote a message)

# 替换开始的尖括号和空行

sed 's/^/> /'

 

# delete leading angle bracket & space from each line (unquote a message)

# 删除开始的尖括号 & 空格

sed 's/^> //'

 

# remove most HTML tags (accommodates multiple-line tags)

# 删除大多数的 HTML 标签(兼容成对的标签)

sed -e :a -e 's/<[^>]*>//g;/</N;//ba'

 

# extract multi-part uuencoded binaries, removing extraneous header

# info, so that only the uuencoded portion remains. Files passed to

# sed must be passed in the proper order. Version 1 can be entered

# from the command line; version 2 can be made into an executable

# Unix shell script. (Modified from a script by Rahul Dhesi.)

 

# 摘录多处加码过的二进制文件内容,去除不相关的头部信息,因此只有加码处理过的部分被保留,文件被 sed 处理时必须以合适的顺序。版本 1 能够从命令行输入,版本 2 得写成 unix 执行脚本(此脚本是从 Rahul Dhesi 那里借用来的)

 

 

 

sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode   # vers. 1

sed '/^end/,/^begin/d' "$@" | uudecode                    # vers. 2

 

# zip up each .TXT file individually, deleting the source file and

# setting the name of each .ZIP file to the basename of the .TXT file

# (under DOS: the "dir /b" switch returns bare filenames in all caps).

# 将每个 .TXT 文件都打包到 .ZIP 文件中,删除源文件在 .ZIP 文件中设置为原来 .TXT 文件加入各自的原文件名(在 DOS 下:“ dir /b” 转换后返回仅有的文件名)

 

echo @echo off >zipup.bat

dir /b *.txt | sed "s/^/(.*/)/.TXT/pkzip -mo /1 /1.TXT/" >>zipup.bat

 

TYPICAL USE: Sed takes one or more editing commands and applies all of

them, in sequence, to each line of input. After all the commands have

been applied to the first input line, that line is output and a second

input line is taken for processing, and the cycle repeats. The

preceding examples assume that input comes from the standard input

device (i.e, the console, normally this will be piped input). One or

more filenames can be appended to the command line if the input does

not come from stdin. Output is sent to stdout (the screen). Thus:

 

 

典型用法: Sed 使用一条或多条编辑命令 而且迭代它们执行。 依次的,对输入的每一行。在所有命令对输入的第一行迭代执行后,那一行将被输出,而输入的第二行将被用来进行循环处理。先前的所有例子都假定输入来自标准输入设备(比如:控制台,正常的管道输入),如果输入不是从标准输入而来那么一个或更多的文件名将会被要求追加到命令行,输出则是被发送到标准输出(屏幕)。到此结束。

 

 

 

 

 

cat filename | sed '10q'        # uses piped input

sed '10q' filename               # same effect, avoids a useless "cat"

sed '10q' filename > newfile    # redirects output to disk

 

For additional syntax instructions, including the way to apply editing

commands from a disk file instead of the command line, consult "sed &

awk, 2nd Edition," by Dale Dougherty and Arnold Robbins (O'Reilly,

1997; http://www.ora.com), "UNIX Text Processing," by Dale Dougherty

and Tim O'Reilly (Hayden Books, 1987) or the tutorials by Mike Arst

distributed in U-SEDIT2.ZIP (many sites). To fully exploit the power

of sed, one must understand "regular expressions." For this, see

"Mastering Regular Expressions" by Jeffrey Friedl (O'Reilly, 1997).

The manual ("man") pages on Unix systems may be helpful (try "man

sed", "man regexp", or the subsection on regular expressions in "man

ed"), but man pages are notoriously difficult. They are not written to

teach sed use or regexps to first-time users, but as a reference text

for those already acquainted with these tools.

 

对于额外的语法指令,包括那些以磁盘文件代替命令行输入的方法请查阅 Dale Dougherty Arnold Robbins 编写的“ sed &awk 第二版” O'Reilly 出版社, 1997 年出版, http://www.ora.com ), Dale Dougherty Tim O'Reilly 编写的“ UNIX Text Processing, ”( Hayden Books, 1987 )或者是浏览 Mike Arst 发布的 U-SEDIT2.ZIP 内容(很多站点可以下载)。要想完全的探索功能强大的 sed ,首先必须理解“正则表达式”。对于这点,请查看 Jeffrey Friedl 的“ Mastering Regular Expressions ”一书( O'Reilly, 1997 )。 Unix 自带的帮助文档也是很有帮助的(试试 man sed,man regexp ),或者有一部分的正则表达式可以在“ man ed ”中找到,但是帮助文档的难于理解是臭名昭著的。他们的撰写目的不是教 sed 的用法也不是给首次使用正则表达式的人所准备,但作为参考这份文档对于已在使用 sed 的人来说是很有价值的。

 

 

 

 

 

QUOTING SYNTAX: The preceding examples use single quotes ('...')

instead of double quotes ("...") to enclose editing commands, since

sed is typically used on a Unix platform. Single quotes prevent the

Unix shell from intrepreting the dollar sign ($) and backquotes

  (`...`), which are expanded by the shell if they are enclosed in

double quotes. Users of the "csh" shell and derivatives will also need

to quote the exclamation mark (!) with the backslash (i.e., /!) to

properly run the examples listed above, even within single quotes.

Versions of sed written for DOS invariably require double quotes

  ("...") instead of single quotes to enclose editing commands.

 

引用语法:

前的例子用单引号代替双引号去关闭命令,自从 Sed 开始在 Unix 平台上广泛的使用。单引号阻止 Unix Shell $ 或是反引号打断,所以被扩展的使用双引号去完成命令 csh 及其衍生工具的用户需要用反斜杠( / )对感叹号(!)进行转义以便能够成功的运行上面所列的例子,即使在使用单引号版本的 sedd 在为 DOS 编写时总是需要双引号去代替双引号将命令完成。

 

 

 

USE OF '/t' IN SED SCRIPTS: For clarity in documentation, we have used

the expression '/t' to indicate a tab character (0x09) in the scripts.

However, most versions of sed do not recognize the '/t' abbreviation,

so when typing these scripts from the command line, you should press

the TAB key instead. '/t' is supported as a regular expression

metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.

 

SED 脚本中使用 /t: 在脚本中为了清楚的说明,我们用表达式 /t 来指明一个制表符( 0x09 )。然而,大多数 sed 版本不认识 /t 这一简写,因此当你在命令行中键入这些脚本时,你应该键入 TAB 键来替代 /t /t 做的正则表达式的 元字符 可以被 awk ,perl,HHsde,sedmod, 以入 GNU sed v3.02.80 所支持。

 

 

 

VERSIONS OF SED: Versions of sed do differ, and some slight syntax

variation is to be expected. In particular, most do not support the

use of labels (:name) or branch instructions (b,t) within editing

commands, except at the end of those commands. We have used the syntax

which will be portable to most users of sed, even though the popular

GNU versions of sed allow a more succinct syntax. When the reader sees

a fairly long command such as this:

 

SED 的版本: sed 的版本有显著的差别,一些轻微的语法变动是很意料之中的。特别的,大多数的版本不支持在命令行中使用标签(:标签名)或分支命令( b,t ),除了在这些命令的末尾。我们使用的语法对大多数用户的 Sed 来说是轻便的,即使最流行的 GNU 版本的 sed 允许一种更加简明的语法。当读者看到一条像下面一样很长的命令:

 

 

   sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d

 

it is heartening to know that GNU sed will let you reduce it to:

当听到 GNU 版本的 Sed 允许你这样简写它:

 

   sed '/AAA/b;/BBB/b;/CCC/b;d'      # or even

   sed '/AAA/|BBB/|CCC/b;d'

 

In addition, remember that while many versions of sed accept a command

like "/one/ s/RE1/RE2/", some do NOT allow "/one/! s/RE1/RE2/", which

contains space before the 's'. Omit the space when typing the command.

 

另外,记住很多版本的 Sed 在接受一条命令如 ”/one/s/RE1/RE2/” 的命令时,有一些却不接受如 ”/one/ s/RE1/RE2/” 这样的命令,那些 s 前面有空格的命令。请忽略这些空格当在键入命令时。

 

OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to

large input files or slow processors or hard disks), substitution will

be executed more quickly if the "find" expression is specified before

giving the "s/.../.../" instruction. Thus:

 

从速度上优化:如果执行速度需要增加(大概是大的输入文件或是处理器和硬盘速度很慢),如果“ find ”表达式放在替换命令“ s/…/…/ ”的前面,速度将会被提升。到此结束。

 

 

   sed 's/foo/bar/g' filename         # standard replace command

   sed '/foo/ s/foo/bar/g' filename   # executes more quickly

   sed '/foo/ s//bar/g' filename      # shorthand sed syntax

 

On line selection or deletion in which you only need to output lines

from the first part of the file, a "quit" command (q) in the script

will drastically reduce processing time for large files. Thus:

 

在行的选择和删除操作中如果你只需要输出文件中的第一部分,一个退出命令( q )将为你大幅节省处理大型文件的时间。至此结束。

 

 

   sed -n '45,50p' filename           # print line nos. 45-50 of a file

   sed -n '51q;45,50p' filename       # same, but executes much faster

 

If you have any additional scripts to contribute or if you find errors

in this document, please send e-mail to the compiler. Indicate the

version of sed you used, the operating system it was compiled for, and

the nature of the problem. Various scripts in this file were written

or contributed by:

 

如果你有额外的脚本想要贡献或是你在此文档中发现任何错误,请发电子邮件联系编译者。请指明你所使用的 sed 版本,编译它所使用的操作系统,这个问题的本质。在这个文档中的这些脚本都是以下人员编写和贡献的:

 

Al Aab <af137@freenet.toronto.on.ca>   # "seders" list moderator

Edgar Allen <era@sky.net>              # various

Yiorgos Adamopoulos <adamo@softlab.ece.ntua.gr>

Dale Dougherty <dale@songline.com>     # author of "sed & awk"

Carlos Duarte <cdua@algos.inesc.pt>    # author of "do it with sed"

Eric Pement <pemente@northpark.edu>    # author of this document

Ken Pizzini <ken@halcyon.com>          # author of GNU sed v3.02

S.G. Ravenhall <stew.ravenhall@totalise.co.uk> # great de-html script

Greg Ubben <gsu@romulus.ncsc.mil>      # many contributions & much help

-------------------------------------------------------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值