sed流式编辑器


sed流式编辑器
 
sed是一个流式编辑器(stream editor)。
 
在sed中,pattern space和hold space为两个缓冲区,开始时数据由标准输入被读入pattern space,经过处理输出到标准输出。hold space只是一个辅助处理的缓冲区,有些命令可以对hold space缓冲区操作,比如H,h,g,G等。
 
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
 
这是命令的标准格式:
 
描述:sed是一个流式编辑器,流式编辑器用来处理对一个输入 流进行处理。就像很多运行脚本编辑的编辑器一样,sed只处理一行输入,因此更高效。但是sed在管道中过滤文本的作用使它明显区别于其他类型编辑器。
 
-n,
 --quiet, --silent
 
抑制pattern space的自动输出功能
 
-e script, --expression=script
 
把script加入到执行命令中
 
-f
 script-file, --file=script-file
 
命令输入来源为文件
 
-i[SUFFIX],
 --in-place[=SUFFIX]
 
原地编辑
 
-l
 N, --line-length=N
 
设定自动换行的长度
 
--posix
 
关闭所有GNU扩展
 
-r, --regexp-extended
 
应用扩展的正则表达式
 
-s, --separate
 
把多个文件当做多个流
 
 -u, --unbuffered
 
每次从文件读入更少数据更频繁的flush到输出
 
如果没有-e 或者-f选项,第一个非选项参数作为sed脚本运行。其余所有的参数都为输入文件,如果没有输入文件,从标准输入读取。
 
COMMAND SYNOPSIS
这只是一个sed的简明教程
 
零地址命令:
 
:label
 
为 b 和 t命令用的label
 
#comment
 
直到下一行命令都为注释(或者-e 命令的末尾)
 
零或者一个地址命令:
 
=
 
打印当前行号
 
a text:
 
加入text,可以用反斜杠换行
 
i text:
 
插入text,可以用反斜杠换行
 
q
 
立即退出sed脚本不处理任何更多的输入,但如果没有禁用自动打印,将打印当前模式空间。
 
Q
 
立即退出sed脚本不处理任何更多的输入
 
r filename
 
加入从filename读入的text
 
R filename
 
加入从filaname读入一行数据
 
Commands which accept address ranges
b label
 
              Branch to label; if label is omitted, branch to end of script.
 
跳转到label执行,如果没有就到最后
 
 t label
 
              If a s/// has done a successful substitution since the last input line was  read  and
 
              since  the  last t or T command, then branch to label; if label is omitted, branch to
 
              end of script.
 
 
自从上次输入或者上次T/t命令后s替换命令执行成功,跳转到label,没哟label,到最后
 
  T label
              If no s/// has done a successful substitution since the last input line was read  and
              since  the  last t or T command, then branch to label; if label is omitted, branch to
              end of script.
自从上次输入或者上次T/t命令后s替换命令执行不成功,跳转到label,没哟label,到最后
 
c text:
 
替换text,可以用反斜杠换行
 
d 从下一个循环开始删除pattern space
 
D删除pattern 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 unambiguous'' 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 current pattern space.
 
       w filename
              Write the current pattern space to filename.
 
 
       W filename
              Write the first line of the current pattern space to filename.
s/regexp/replacement/
              Attempt to match regexp against the pattern space.  If successful, replace that  por-
              tion  matched  with replacement.  The replacement may contain the special 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.
用replacement替换regexp匹配的字段,replacement中可能包含&字符代指与regexp匹配的字符串,用\1到\9代指相应匹配的子段
 
y/source/dest/
              Transliterate  the characters in the pattern space which appear in source to the cor-
              responding character in dest.
 
Addresses
如果一个地址参数也没有,对所有行进行处理,如果有一个地址,对哪一行进行处理,如果有两个地址,对两行之间的行处理,哪怕参数2小于参数1,如果地址2是一个正则表达式,不会匹配第一个参数行。
 
前面加!符号,反向选择
 
number 具体一行
 
 first~step
 
从first行开始的步长为step的行
 
$ 最后一行
 
 /regexp/ 与此正则表达式匹配的一行
 
\cregexpc  c可以为任何字符,同上,只不过把换一种形式
 
addr1,+N 从addr1开始的N行
 
addr1,~N 从addr1开始的N的整数倍行

相关内容

    暂无相关文章