Sed 的man手册参数详细解释(一)

来源:互联网 发布:linux安装tar.gz文件 编辑:程序博客网 时间:2024/04/28 18:51

 

NAME

sed - stream editor for filtering and transforming text

SYNOPSIS

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

DESCRIPTIONSed is a stream editor. A stream editor is used to perform 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.

中文翻译:Sed 是一个流编辑器,用于处理来自文件或者管道的标准文本的转换工作,尽管在某些方面类似于允许脚本编辑的编辑器,但是由于sed仅仅对其输入进行一次扫面,因此比起其他交互编辑器,sed更加高效。同时由于sed过滤来自管道中的文本,使得它变得与众不同。

补充:Sed把当前处理的行存储在称为模式空间(pattern space)的临时缓冲区中。一旦sed完成对模式空间按中的行的处理,模式空间中的行就被送往屏幕(除非命令是删除行或者打印到打印机),行被处理完成以后,就被移出模式空间,程序接着读入下一行,处理、显示、移出……文件输入的最后一行被处理完以后,sed结束,通过在临时缓冲区存储每一行,然后再缓冲区中操作该行,保证原始文件不会被破坏。

-n, --quiet, --silent

suppress automatic printing of pattern space

取消将模式空间中的内容自动打印出来。

解释:也就是说除非指定选项-n,否则sed每执行到命令最后,都会输出存在于模式空间中的行,也就是处理完缓冲到模式空间中的行以后自动输出模式空间的内容,同时如果有p指令,还会在输出p指令指定的行,而当在命令行中使用-n选项以后,这种自动工作被禁止,如果使用了p(小写)指令了以后,只会输出p指令指定的行。

/ +++++++++++++++++++++++++++++++++++++++例子1++++++++++++++++++++++++++++++++++++++

sed.txt的内容如下:

This is the 1st line.

This is the 2nd line.

sed命令:sed –e ‘1p’ sed.txt 由于不加-n选项,默认的每处理一个模式空间(数据行)sed自动将处理完的数据行打印才继续读取下一行,如果指定了-p选项,还会再打印p指定的行(这里p指定第一行)。

所以执行完以上命令后显示:

This is the 1st line.

This is the 1st line.

This is the 2nd line.

如果加了-n选项,那个默认的打印动作被禁止,如果指定了-p选项,会打印p指定的行(这里p指定第一行),命令sed –ne ‘1p’ sed.txt 执行以后只显示:This is the 1st line.这一行。

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

-e script, --expression=script

add the script to the commands to be executed

以选项指定的script来处理文本。

补充:如果命令行上只有一个指令的时候可以不用写-e,但是如果有多个指令的话一定要在每个指令的前面加-e选项;sed指令用引号单引号和双引号指定都可以,或者可以不用引号,这个时候如果指令中有特殊字符,要用反斜杠/ 来屏蔽掉特殊字符比如“空格”、“;”等,所以sed命令:sed -e‘1p’sed.txt 可以这样写: sed ‘1p’sed.txt 不用指定-e,直接加命令‘1p’或者:sed 1p sed.txt不用加引号,如果指令中有特殊字符的话一定要用反斜杠来转义比如:sed 1s/ line/LINE/p sed.txt 会出现unterminated `s' command的错误,如果加了反斜杠sed 1s// line/LINE/p sed.txt 就可以正确运行,但是建议用单引号,同时写上-e,否则阅读难度增大。哦差点忘了,如果指令有多个指令,可以有三种书写方式:1.多个指令写在一起,用分号分隔;2.多个指令分开写,但是要在每个指令前放置-e;3.使用Bash的分行指令功能,注意在输入单引号后按回车键(Enter)就会出现多行输入的提示符“>”。

/ +++++++++++++++++++++++++++++++++++++++例子2++++++++++++++++++++++++++++++++++++++

sed.txt的内容如下:

This is the 1st line.

This is the 2nd line.

1.sed命令:sed –ne ‘1p’ sed.txt 、sed –n ‘1p’ sed.txt 、sed –n 1p sed.txt执行以后显示

This is the 1st line.

2.sed命令:sed -ne ‘1s/ line/LINE/p’ sed.txt 、sed -ne 1s// line/LINE/p sed.txt执行后显示This is the 1stLINE.

3.sed命令:

sed -ne '1s/1st/first/g;2s/2nd/second/g;p' sed.txt

或者sed –n -e '1s/1st/first/g' -e '2s/2nd/second/g' -e 'p' sed.txt

或者:sed –n –e ‘ #按下回车键

>1s/1st/first/g #再按下回车键

>2s/2nd/second/g #同上

>p #同上

>’ sed.txt

以上三种书写形式的命令执行后都会显示:

This is the first line.

This is the second line.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

-f script-file, --file=script-file

add the contents of script-file to the commands to be executed

以选项指定的script-file文件来处理文本,也就是可以将sed程序写到文件中,然后通过-f选项指定sed程序。

补充:一条sed命令可以同时使用-e 和 –f选项。脚本文件中每个指令写在一行或者几个指令写在一行每个指令之间用分号隔开。建议一个指令写一行!

/ +++++++++++++++++++++++++++++++++++++++例子3++++++++++++++++++++++++++++++++++++++

sed.txt的内容如下:

This is the 1st line.

This is the 2nd line.

This is the 3rd line.

sed命令:sed –n –f sed.script sed.txt 其中sed.script的内容如下:

#删除第一行

1d

#在第三行前面插入以下一行数据

3i /

This is the insert line

# 打印当前模式空间的内容

P

执行命令后显示结果如下:

This is the 2nd line.

This is the insert line

This is the 3rd line.

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

原创粉丝点击