Linux -- sed命令(3)

8 使用 sed 处理文件
替换命令包含一些可以用于文件的标记。还有一些sed编辑器命令也可以实现同样的目标,不需要非得替换文本。

8.1 写入文件
w 命令用来向文件写入行。该命令的格式如下:

[address]sed w filename

下面的例子是将数据流中的前两行打印到一个文本文件中。

$ sed -n ‘1,2w test.txt’ data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
$ cat test.txt
This is line number 1.
This is line number 2.
如果要根据一些公用的文本值从主文件中创建一份数据文件,比如下面的邮件列表中的,那么 w 命令会非常好用。

$ cat data11.txt
Blum, R Browncoat
McGuiness, A Alliance
Bresnahan, C Browncoat
Harken, C Alliance
$ sed -n ‘/Browncoat/w Browncoats.txt’ data11.txt
$ cat Browncoats.txt
Blum, R Browncoat
Bresnahan, C Browncoat
sed编辑器会只将包含文本模式的数据行写入目标文件。

8.2 从文件读取数据
读取( read )命令( r )允许你将一个独立文件中的数据插入到数据流中。读取命令的格式如下:

[address]r filename

filename 参数指定了数据文件的绝对路径或相对路径。你在读取命令中使用地址区间,只能指定单独一个行号或文本模式地址。sed编辑器会将文件中的文本插入到指定地址后。

$ cat data12.txt
This is an added line.
This is the second added line.
$ sed ‘3r data12.txt’ data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an added line.
This is the second added line.
This is line number 4.
sed编辑器会将数据文件中的所有文本行都插入到数据流中。同样的方法在使用文本模式地址时也适用。

$ sed ‘/number 2/r data12.txt’ data6.txt
This is line number 1.
This is line number 2.
This is an added line.
This is the second added line.
This is line number 3.
This is line number 4.
$
如果你要在数据流的末尾添加文本,只需用美元符地址符就行了。

$ sed ‘$r data12.txt’ data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is an added line.
This is the second added line.
读取命令的另一个很酷的用法是和删除命令配合使用:利用另一个文件中的数据来替换文件中的占位文本。举例来说,假定你有一份套用信件保存在文本文件中:

$ cat notice.std
Would the following people:
LIST
please report to the ship’s captain.
$ cat data11.txt
Blum, R Browncoat
McGuiness, A Alliance
Bresnahan, C Browncoat
Harken, C Alliance
套用信件将通用占位文本 LIST 放在人物名单的位置。要在占位文本后插入名单,只需读取命令就行了。但这样的话,占位文本仍然会留在输出中。要删除占位文本的话,你可以用删除命令。结果如下:

$ sed '/LIST/{
r data11.txt

d
}’ notice.std
Would the following people:
Blum, R Browncoat
McGuiness, A Alliance
Bresnahan, C Browncoat
Harken, C Alliance
please report to the ship’s captain.
首先使用文本寻址,在占位文本LIST之后追加data11.txt的内容,之后d命令删除占位文本LIST,现在占位文本已经被替换成了数据文件中的名单。

三、多行数据处理命令
有时,我们并不是仅仅对每一行单独地进行操作,而是进行多行操作。比如查找一串字符,但这个字符可能分布在两行甚至多行;再比如,你要根据某一行的特征对相邻的几行进行操作,等等。这时你就要用到多行操作命令了。

sed编辑器包含了三个可用来处理多行文本的特殊命令。

N :将数据流中的下一行加进来创建一个多行组(multiline group)来处理。
D :删除多行组中的一行。
P :打印多行组中的一行。
后面几节将会进一步讲解这些多行命令并向你演示如何在脚本中使用它们。

1 next命令
在讲解多行 next 命令之前,首先需要看一下单行版本的 next 命令是如何工作的,然后就比较容易理解多行版本的 next 命令是如何操作的了。

1.1 单行的 next 命令
n 命令可实现对匹配行的下一行文本进行处理,而不用重新回到命令的最开始再执行一遍。

在这个例子中,你有个数据文件,共有5行内容,其中的两行是空的。目标是删除首行之后的一行空白行,而留下其他的空白行。如果写一个删掉空白行的sed脚本,你会删掉两个空白行。

$ cat data1.txt
This is the header line.
This is a data line.
This is the last line.
$ sed ‘/^$/d’ data1.txt
This is the header line.
This is a data line.
This is the last line.
由于要删除的行是空行,没有任何能够标示这种行的文本可供查找。解决办法是用 n 命令。在这个例子中,脚本要查找含有单词header的那一行。找到之后, n 命令会让sed编辑器移动到文本的下一行,也就是那个空行。

$ sed ‘/header/{n ; d}’ data1.txt
This is the header line.
This is a data line.
This is the last line.
这时,sed编辑器会继续执行命令列表,该命令列表使用 d 命令来删除空白行。sed编辑器执行完命令脚本后,会从数据流中读取下一行文本,并从头开始执行命令脚本。因为sed编辑器再也找不到包含单词header的行了。所以也不会有其他行会被删掉。

1.2 合并文本行
了解了单行版的 next 命令,现在来看看多行版的。单行 next 命令会将数据流中的下一文本行移动到sed编辑器的工作空间(称为模式空间)。多行版本的 next 命令(用大写N)会将下一文本行添加到模式空间中已有的文后。这样的作用是将数据流中的两个文本行合并到同一个模式空间中。文本行仍然用换行符分隔,但sed编辑器现在会将两行文本当成一行来处理。

如果要在数据文件中查找一个可能会分散在两行中的文本短语的话,这是个很实用的应用程序。这里有个例子。

$ cat data3.txt
On Tuesday, the Linux System
Administrator’s group meeting will be held.
All System Administrators should attend.
Thank you for your attendance.
$ sed ‘N ; s/System Administrator/Desktop User/’ data3.txt
On Tuesday, the Linux System
Administrator’s group meeting will be held.
All Desktop Users should attend.
Thank you for your attendance.
替换命令会在文本文件中查找特定的双词短语 System Administrator 。如果短语在一行中的话,事情很好处理,替换命令可以直接替换文本。但如果短语分散在两行中的话,替换命令就没法识别匹配的模式了。

这时 N 命令就可以派上用场了。同时也要加上短语不是分布于两行的情况。

$ sed 'N

s/System\nAdministrator/Desktop\nUser/
s/System Administrator/Desktop User/
’ data3.txt’ data3.txt
On Tuesday, the Linux Desktop User’s group meeting will be held.
All Desktop Users should attend.
Thank you for your attendance.
用 N 命令将发现第一个单词的那行和下一行合并后,即使短语内出现了换行,你仍然可以找到它。

发布了41 篇原创文章 · 获赞 0 · 访问量 503

猜你喜欢

转载自blog.csdn.net/m0_46560389/article/details/105256990