您的位置:首页 > 运维架构 > Shell

linux shell编程指南第十章------sed 用法介绍

2013-11-28 21:19 603 查看
s e d是一个非交互性文本流编辑器。它编辑文件或标准输入导出的文本拷贝。标准输入可

能是来自键盘、文件重定向、字符串或变量,或者是一个管道的文本。s e d可以做些什么呢?

别忘了,Vi也是一个文本编辑器。s e d可以随意编辑小或大的文件,有许多s e d命令用来编辑、

删除,并允许做这项工作时不在现场。s e d一次性处理所有改变,因而变得很有效,对用户来

讲,最重要的是节省了时间。

本章内容有:

• 抽取域。

• 匹配正则表达式。

• 比较域。

• 增加、附加、替换。

• 基本的s e d命令和一行脚本。

可以在命令行输入s e d命令,也可以在一个文件中写入命令,然后调用s e d,这与a w k基本

相同。使用s e d需要记住的一个重要事实是,无论命令是什么, s e d并不与初始化文件打交道,

它操作的只是一个拷贝,然后所有的改动如果没有重定向到一个文件,将输出到屏幕。

因为s e d是一个非交互性编辑器,必须通过行号或正则表达式指定要改变的文本行。

本章介绍s e d用法和功能。本章大多编写的是一行命令和小脚本。这样做可以慢慢加深对

s e d用法的了解,取得宝贵的经验,以便最终自己编出大的复杂s e d脚本。

和g r e p与a w k一样, s e d是一种重要的文本过滤工具,或者使用一行命令或者使用管道与

g r e p与a w k相结合。


s e d从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲

区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复

此过程直到命令结束。


调用sed:



调用s e d有三种方式:在命令行键入命令;将s e d命令插入脚本文件,然后调用s e d;将s e d

命令插入脚本文件,并使s e d脚本可执行。


使用s e d命令行格式为:

sed [选项] s e d命令输入文件。

记住在命令行使用s e d命令时,实际命令要加单引号。s e d也允许加双引号。

使用s e d脚本文件,格式为:

sed [选项] -f sed脚本文件输入文件

要使用第一行具有s e d命令解释器的s e d脚本文件,其格式为:

s e d脚本文件[选项] 输入文件

不管是使用s h e l l命令行方式或脚本文件方式,如果没有指定输入文件, s e d从标准输入中

接受输入,一般是键盘或重定向结果。

s e d选项如下:

n 不打印;s e d不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑)。p命令可以

用来打印编辑行。

c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条s e d命令,

此选项无用,但指定它也没有关系。

f 如果正在调用s e d脚本文件,使用此选项。此选项通知s e d一个脚本文件支持所有的s e d

命令,例如:sed -f myscript.sed input_file,这里m y s c r i p t . s e d即为支持s e d命令的文件。

由于不接触初始化文件,如果想要保存改动内容,简单地将所有输出重定向到一个文件

即可。下面的例子重定向s e d命令的所有输出至文件‘ m y o u t f i l e’,当对结果很满意时使用这

种方法。

sed 'some-sed-commands' input-file >myoutfile

s e d浏览输入文件时,缺省从第一行开始,有两种方式定位文本:

1) 使用行号,可以是一个简单数字,或是一个行号范围。

2 ) 使用正则表达式,怎样构建这些模式请参见第7章。

表1 0 - 1给出使用s e d定位文本的一些方式。







sed实验的文件如下:

[root@localhost huangcd]# cat quote.txt 
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.


s e d识别任何基本正则表达式和模式及其行匹配规则。记住规则之一是:如果要定位一特

殊字符,必须使用( \)屏蔽其特殊含义。


打印文件的第二行。n 不打印;s e d不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑)。p命令可以

用来打印编辑行。

[root@localhost huangcd]# sed -n '2p' quote.txt

It was an evening of splendid music and company.

[root@localhost huangcd]# sed '2p' quote.txt

The honeysuckle band played all night long for only $90.

It was an evening of splendid music and company.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neave was in attendance.

可以指定行的范围,现打印1到3行,用逗号分隔行号。

[root@localhost huangcd]# sed -n '1,3p' quote.txt

The honeysuckle band played all night long for only $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

假定要匹配单词N e a v e,并打印此行,方法如下。使用模式/ p a t t e r n /格式,这里为/ N e a v e /。

[root@localhost huangcd]# sed -n '/Neave/'p quote.txt

The local nurse Miss P.Neave was in attendance.

使用模式和行号进行查询:

为编辑某个单词浏览一个文件时, s e d返回包含指定单词的许多行。怎样使返回结果更精

确以满足模式匹配呢?可以将行号和模式结合使用。下面这个例子,假定要改动文件q u o t e . t x t

最后一行中的单词t h e,使用s e d查询t h e,返回两行:

[root@localhost huangcd]# sed -n '/The/'p quote.txt

The honeysuckle band played all night long for only $90.

The local nurse Miss P.Neave was in attendance.

使用模式与行号的混合方式可以剔除第一行,格式为l i n e n u m b e r, / p a t t e r n /。逗号用来分

隔行号与模式开始部分。为达到预期结果,使用4 , / t h e /。意即只在第四行查询模式t h e,命令

如下:

[root@localhost huangcd]# sed -n '4,/The/'p quote.txt

The local nurse Miss P.Neave was in attendance.

匹配元字符$前,必须使用反斜线\屏蔽其特殊含义。模式为/\$/ p。

[root@localhost huangcd]# sed -n '/\$/'p quote.txt

The honeysuckle band played all night long for only $90.

要打印整个文件,只需将行范围设为第一行到最后一行1 , $。$意为最后一行。

[root@localhost huangcd]# sed -n '1,$p' quote.txt

The honeysuckle band played all night long for only $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neave was in attendance.

匹配任意字母,后跟任意字母的0次或多次重复,并以i n g结尾,模式为/ . * i n g /。可以使用

这个模式查询以i n g结尾的任意单词。

[root@localhost huangcd]# sed -n '/.*ing/'p quote.txt

It was an evening of splendid music and company.

要打印文件第一行,使用行号:

[root@localhost huangcd]# sed -n '1p' quote.txt

The honeysuckle band played all night long for only $90.

[root@localhost huangcd]# sed '1p' quote.txt

The honeysuckle band played all night long for only $90.

The honeysuckle band played all night long for only $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neave was in attendance.

要打印最后一行,使用$。$是代表最后一行的元字符。

[root@localhost huangcd]# sed -n '$p' quote.txt

The local nurse Miss P.Neave was in attendance.

要打印行号,使用等号=。打印模式匹配的行号,使用格式/ p a t t e r n / =。

[root@localhost huangcd]# sed -n '/music/=' quote.txt

2

[root@localhost huangcd]# sed '/music/=' quote.txt

The honeysuckle band played all night long for only $90.

2

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neave was in attendance.

整个文件都打印出来,并且匹配行打印了行号。如果只关心实际行号,使用- n选项。

如果只打印行号及匹配行,必须使用两个s e d命令,并使用e选项。第一个命令打印模式

匹配行,第二个使用=选项打印行号,格式为sed -n -e /pattern/p -e /pattern/=。

[root@localhost huangcd]# sed -n -e '/music/p' -e '/music/=' quote.txt

It was an evening of splendid music and company.

2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: