Shell学习笔记(8)——sed与gawk基础

sed与gawk的基本用法

Linux系统中有两个常用的命令行编辑器,分别为sedgawk

sed编辑器

sed编辑器被称作流编辑器(streameditor),和普通的交互式文本编辑器有所不同。在交互式文本编辑器中(比如vim),用户可以用键盘命令来交互式地插入、删除或替换数据中的文本。而流编辑器则会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流。sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。可以简单地理解为,流编辑器像一种滤器,数据进入后,会以一定标准的格式出来,sed编辑器会执行下列操作:

  1. 一次从输入中读取一行数据。
  2. 根据所提供的编辑器命令匹配数据。
  3. 按照命令修改流中的数据。
  4. 将新的数据输出到STDOUT。

在流编辑器将所有命令与一行数据匹配完毕后,它会读取下一行数据并重复这个过程。在流编辑器处理完流中的所有数据行后,它就会终止。由于命令是按顺序逐行给出的,sed编辑器只需对数据流进行一遍处理就可以完成编辑操作。

sed命令的格式如下为sed options script file,其中sed的选项如下所示:

选项 描述
-e script 在处理输入时,将script中指定的命令添加到已有的命令中
-f file 在处理输入时,将file中指定的命令添加到已有的命令中
-n 不产生命令输出,使用print命令来完成输出

script参数指定了应用于流数据上的单个命令。如果需要用多个命令,要么使用-e选项在命令行中指定,要么使用-f选项在单独的文件中指定。

在命令行定义编辑器命令

默认情况下,sed编辑器会将指定的命令应用到STDIN输入流上。这样你可以直接将数据通过管道输入sed编辑器处理,如下所示:

1
2
biotest@ubuntu:~/sed$ echo "This is a test"|sed 's/test/big test/'
This is a big test

在这个例子中,sed编辑器使用了s命令。s命令会用斜线间指定的第二个文本字符串(这里是big test)来替换第一个文本字符串(test)模式。在本例中是big test替换了test。在运行这个例子时,结果应该立即就会显示出来,再看一个案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
biotest@ubuntu:~/sed$ cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dogs.
biotest@ubuntu:~/sed$ sed 's/dog/cat/' data1.txt
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.

sed命令几乎瞬间就执行完并返回数据。在处理每行数据的同时,结果也显示出来了。可以在sed编辑器处理完整个文件之前就开始观察结果。需要注意的是,sed编辑器并不会修改文本文件的数据。它只会将修改后的数据发送到STDOUT,也可以发送到某个文件。如果你查看原来的文本文件,它仍然保留着原始数据,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
biotest@ubuntu:~/sed$ sed 's/dog/cat/' data1.txt > data1b.txt
biotest@ubuntu:~/sed$ cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
biotest@ubuntu:~/sed$ cat data1b.txt
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.

在命令行使用多个编辑器命令

要在sed命令行上执行多个命令时,只要用-e选项就可以了,如下所示:

1
2
3
4
5
6
7
biotest@ubuntu:~/sed$ sed -e 's/brown/green/;s/dog/cat/' data1.txt
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.

两个命令都作用到文件中的每行数据上。命令之间必须用分号隔开,并且在命令末尾和分号之间不能有空格。如果不想用分号,也可以用bash shell中的次提示符来分隔命令。只要输入第一个单引号标示出sed程序脚本的起始(sed编辑器命令列表),bash会继续提示你输入更多命令,直到输入了标示结束的单引号,如下所示:

1
2
3
4
5
6
7
8
biotest@ubuntu:~/sed$ sed -e '
> s/brown/green/
> s/fox/elephant/
> s/dog/cat/' data1.txt
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.

从文件中读取编辑器命令

如果有大量要处理的sed命令,那么将它们放进一个单独的文件中,可以在sed命令中用-f选项来指定文件,如下所示:

1
2
3
4
5
6
7
8
9
biotest@ubuntu:~/sed$ cat script1.sed
s/brown/green/
s/fox/elephant/
s/dog/cat/
biotest@ubuntu:~/sed$ sed -f script1.sed data1.txt
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.

在这种情况下,不用在每条命令后面放一个分号。sed编辑器知道每行都是一条单独的命令。跟在命令行输入命令一样,sed编辑器会从指定文件中读取命令,并将它们应用到数据文件中的每一行上。

gawk 程序

gawk程序是另外的一个处理文本文件的工具,它是Unix原始awk程序的GUN版本,它能提供一个类编程环境来修改和重新组织文件中的数据。 gawk的强大之处在于程序脚本,用户可以写脚本来读取文本行的数据,然后处理并显示数据,创
建任何类型的输出报告。 Ubuntu本身并没有带gawk工具,需要自己安装,即get apt install gawk。在gawk编程语言中,用户可以做下面的事情:

  1. 定义变量来保存数据;
  2. 使用算术和字符串操作符来处理数据;
  3. 使用结构化编程概念(比如 if-then 语句和循环)来为数据处理增加处理逻辑;
  4. 通过提取数据文件中的数据元素,将其重新排列或格式化,生成格式化报告。

gawk程序通常用来从大文本文件中提取数据元素,并将它们格式化成可读的报告。其中最常用的就是格式化日志文件。在日志文件中找出错误行会很难,gawk程序可以让用户从日志文件中过滤出需要的数据元素,然后将其格式化,使得重要的数据更易于阅读。

gawk的用法

gawk的使用格式为gawk options program file,可用选项如下表所示:

选项 描述
-F fs 指定行中划分数据字段的字段分隔符
-f file 从指定的文件中读取程序
-v var=value 定义gawk程序中的一个变量及其默认值
-mf N 指定要处理的数据文件中的最大字段数
-mr N 指定数据文件中的最大数据行数
-W keyword 指定gawk的兼容模式或警告等级

从命令行读取程序脚本

gawk程序脚本用一对花括号来定义。你必须将脚本命令放到两个花括号({})中。如果你错误地使用了圆括号来包含gawk脚本,就会得到一条类似于下面的错误提示,如下所示:

1
2
3
biotest@ubuntu:~/sed$ gawk '(print "Hello World!"}'
gawk: cmd. line:1: (print "Hello World!"}
gawk: cmd. line:1: ^ syntax error

由于gawk命令行假定脚本是单个文本字符串,必须将脚本放到单引号中。下面的例子在命令行上指定了一个简单的gawk程序脚本:

1
2
3
biotest@ubuntu:~/sed$ gawk '{print "Hello World!"}'
Hello World!
Hello World!

这个程序脚本定义了一个命令:print命令。这个命令会将文本打印到STDOUT。如果尝试运行这个命令,什么都不会发生。原因在于没有在命令行上指定文件名,所以gawk程序会从STDIN接收数据。在运行这个程序时,它会一直等待从STDIN输入的文本。如果输入一行文本并按下回车键,gawk会对这行文本运行一遍程序脚本。跟sed编辑器一样,gawk程序会针对数据流中的每行文本执行程序脚本。由于程序脚本被设为显示一行固定的文本字符串,因此不管在数据流中输入什么文本,都会得到同样的文本输出。

要终止这个gawk程序,你必须表明数据流已经结束了。bash shell提供了一个组合键来生成EOF(End-of-File)字符。Ctrl+D组合键会在bash中产生一个EOF字符。这个组合键能够终止该gawk程序并返回到命令行界面提示符下。

使用数据字段变量

gawk的主要特性之一是其处理文本文件中数据的能力。它会自动给一行中的每个数据元素分配一个变量。默认情况下,gawk会将如下变量分配给它在文本行中发现的数据字段:

  1. $0 代表整个文本行;
  2. $1 代表文本行中的第1个数据字段;
  3. $2 代表文本行中的第2个数据字段;
  4. $n 代表文本行中的第n个数据字段。

在文本行中,每个数据字段都是通过字段分隔符划分的。gawk在读取一行文本时,会用预定义的字段分隔符划分每个数据字段。gawk中默认的字段分隔符是任意的空白字符(例如空格或制表符)。在下面的例子中,gawk程序读取文本文件,只显示第1个数据字段的值。

1
2
3
4
5
6
7
8
biotest@ubuntu:~/sed$ cat data2.txt
One line of test text.
Two lines of test text.
Three lines of test text.
biotest@ubuntu:~/sed$ gawk '{print $1}' data2.txt
One
Two
Three

该程序用$1字段变量来仅显示每行文本的第1个数据字段。如果你要读取采用了其他字段分隔符的文件,可以用-F选项指定,如下所示:

1
2
3
4
5
6
7
8
9
10
11
biotest@ubuntu:~/sed$ gawk -F: '{print $1}' /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
...

这个简短的程序显示了系统中密码文件的第1个数据字段。由于/etc/passwd文件用冒号来分隔数字字段,因而如果要划分开每个数据元素,则必须在gawk选项中将冒号指定为字段分隔符,其中-F:就是指定冒号为分隔符。

在程序脚本中使用多个命令

gawk编程语言允许多条命令组合成一个正常的程序。要在命令行上的程序脚本中使用多条命令,只要在命令之间放个分号即可,如下所示:

1
2
biotest@ubuntu:~/sed$ echo "My name is Zhang" |gawk '{$4="Li";print $0}'
My name is Li

第一条命令会给字段变量$4赋值。第二条命令会打印整个数据字段。注意,gawk程序在输出中已经将原文本中的第四个数据字段替换成了新值。也可以用次提示符一次一行地输入程序脚本命令。

1
2
3
4
5
biotest@ubuntu:~/sed$ gawk '{
> $4="Li"
> print $0}'
My name is Zhang
My name is Li

在用了表示起始的单引号后,bashs hell会使用次提示符来提示你输入更多数据。可以每次在每行加一条命令,直到输入了结尾的单引号。因为没有在命令行中指定文件名,gawk程序会从STDIN中获得数据。当运行这个程序的时候,它会等着读取来自STDIN的文本。要退出程序,只需按下Ctrl+D组合键来表明数据结束。

从文件中读取程序

gawk编辑器允许将程序存储到文件中,然后再在命令行中引用,如下所示:

1
2
3
4
5
6
7
8
9
biotest@ubuntu:~/sed$ cat script2.gawk
{print $1 "'s home directory is " $6}
biotest@ubuntu:~/sed$ gawk -F: -f script2.gawk /etc/passwd
root's home directory is /root
daemon's home directory is /usr/sbin
bin's home directory is /bin
sys's home directory is /dev
sync's home directory is /bin
......

在这个案例中,script2.gawk程序脚本会再次使用print命令打印/etc/passwd文件的主目录数据字段(字段变量$6),以及userid数据字段(字段变量$1`)。可以在程序文件中指定多条命令。要这么做的话,只要一条命令放一行即可,不需要用分号,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
biotest@ubuntu:~/sed$ cat script3.gawk
{
text = "'s home directory is " # 变量未使用美元符号,需要注意
print $1 text $6
}
biotest@ubuntu:~/sed$ gawk -F: -f script3.gawk /etc/passwd
root's home directory is /root
daemon's home directory is /usr/sbin
bin's home directory is /bin
sys's home directory is /dev
sync's home directory is /bin

script3.gawk程序脚本定义了一个变量来保存print命令中用到的文本字符串。注意,gawk程序在引用变量值时并未像shell脚本一样使用美元符。

在处理数据前运行脚本

gawk可以指定程序脚本何时运行。默认情况下,gawk会从输入中读取一行文本,然后针对该行的数据执行程序脚本。有时可能需要在处理数据前运行脚本,比如为报告创建标题。BEGIN关键字就是用来做这个的。它会强制gawk在读取数据前执行BEGIN关键字后指定的程序脚本。

1
2
biotest@ubuntu:~/sed$ gawk 'BEGIN {print "Hello World!"}'
Hello World!

这次print命令会在读取数据前显示文本。但在它显示了文本后,它会快速退出,不等待任何数据。如果想使用正常的程序脚本中处理数据,必须用另一个脚本区域来定义程序,看下面的案例:

1
2
3
4
5
6
7
8
9
10
biotest@ubuntu:~/sed$ cat data3.txt
Line 1
Line 2
Line 3
biotest@ubuntu:~/sed$ gawk 'BEGIN {pirnt "The data3 File Contents:"}
> {print $0}' data3.txt
Line 1
Line 2
Line 3

在gawk执行了BEGIN脚本后,它会用第二段脚本来处理文件数据。这么做时要小心,两段脚本仍然被认为是gawk命令行中的一个文本字符串。你需要相应地加上单引号。

在处理数据后运行脚本

与BEGIN关键字类似,END关键字允许你指定一个程序脚本,gawk会在读完数据后执行它,如下所示:

1
2
3
4
5
6
7
biotest@ubuntu:~/sed$ gawk 'BEGIN {pirnt "The data3 File Contents:"}
> {print $0}
> END {print "End of FIle"}' data3.txt
Line 1
Line 2
Line 3
End of FIle

当gawk程序打印完文件内容后,它会执行END脚本中的命令。这是在处理完所有正常数据后给报告添加页脚的最佳方法。可以将所有这些内容放到一起组成一个脚本文件,用它从一个简单的数据文件中创建一份完整的报告,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
biotest@ubuntu:~/sed$ cat script4.gawk
BEGIN {
print "The latest list of users and shells"
print " UserID \t Shell"
print "-------- \t --------"
FS=":"
}
{
print $1 " \t " $7
}
END {
print "This concludes the listing"
}
biotest@ubuntu:~/sed$ gawk -f script4.gawk /etc/passwd
The latest list of users and shells
UserID Shell
-------- --------
root /bin/bash
daemon /usr/sbin/nologin
bin /usr/sbin/nologin

这个脚本用BEGIN脚本来为报告创建标题。它还定义了一个叫作FS的特殊变量。这是定义字段分隔符的另一种方法。这样就不用依靠脚本用户在命令行选项中定义字段分隔符了。BEGIN脚本创建了标题,程序脚本处理特定数据文件(/etc/passwd)中的信息,END脚本生成页脚。

sed 编辑器基础

更多的替换选项

s命令(substitute)用来在行中替换文本。这个命令还有另外一些选项。

替换标记

先看一个案例,如下所示:

1
2
3
4
5
6
7
biotest@ubuntu:~/sed$ cat data4.txt
This is a test of the test script.
This is the second test of the test script.
biotest@ubuntu:~/sed$ sed 's/test/trial/' data4.txt
This is a trial of the test script.
This is the second trial of the test script.

替换命令在替换多行中的文本时能正常工作,但默认情况下它只替换每行中出现的第一处。要让替换命令能够替换一行中不同地方出现的文本必须使用替换标记(substitution flag)。替换标记会在替换命令字符串之后设置,格式为s/pattern/replacement/flags,有4种可用的替换标记:

  1. 数字,表明新文本将替换第几处模式匹配的地方;
  2. g ,表明新文本将会替换所有匹配的文本;
  3. p ,表明原先行的内容要打印出来;
  4. w file ,将替换的结果写到文件中。
数字替换标记

在第一类替换中,可以指定sed编辑器用新文本替换第几处模式匹配的地方,如下所示:

1
2
3
biotest@ubuntu:~/sed$ sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.

这个脚本运行的结果是:sed编辑器只替换每行中第2次出现的匹配模式。

g替换标记

g替换标记使你能替换文本中匹配模式所匹配的每处地方,如下所示:

1
2
3
biotest@ubuntu:~/sed$ sed 's/test/trial/g' data4.txt
This is a trial of the trial script.
This is the second trial of the trial script.

如结果所示,使用了g替换标记后,文本中所有的地方都被替换了。

p替换标记

p替换标记会打印与替换命令中指定的模式匹配的行。这通常会和sed的-n选项一起使用,如下所示:

1
2
3
4
5
biotest@ubuntu:~/sed$ cat test5.txt
This is a test line.
This is a different line.
biotest@ubuntu:~/sed$ sed -n 's/test/trial/p' test5.txt
This is a trial line.

-n选项将禁止sed编辑器输出。但p替换标记会输出修改过的行。将二者配合使用的效果就是只输出被替换命令修改过的行,第二行并没有被替换,也就不输出了。

w替换标记

w 替换标记会产生同样的输出,不过会将输出保存到指定文件中,如下所示:

1
2
3
4
5
biotest@ubuntu:~/sed$ sed 's/test/trial/w test.txt' test5.txt
This is a trial line.
This is a different line.
biotest@ubuntu:~/sed$ cat test.txt
This is a trial line.

sed编辑器的正常输出是在STDOUT中,而只有那些包含匹配模式的行才会保存在指定的输出文件中。

替换字符

有时会在文本字符串中遇到一些不太方便在替换模式中使用的字符。Linux中一个常见的例子就是正斜线(/)。替换文件中的路径名会比较麻烦。比如想用C shell替换/etc/passwd文件中的bash shell,必须这么做:

1
2
3
4
5
biotest@ubuntu:~/sed$ sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd
root:x:0:0:root:/root:/bin/csh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin

由于正斜线通常用作字符串分隔符,因而如果它出现在了模式文本中的话,必须用反斜线来转义。这通常会带来一些困惑和错误。 要解决这个问题,sed编辑器允许选择其他字符来作为替换命令中的字符串分隔符:

1
2
3
4
biotest@ubuntu:~/sed$ sed 's!/bin/bash!/bin/csh!' /etc/passwd
root:x:0:0:root:/root:/bin/csh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin

在这个例子中,感叹号被用作字符串分隔符,这样路径名就更容易阅读和理解了。

使用地址

默认情况下,在sed编辑器中使用的命令会作用于文本数据的所有行。如果只想将命令作用于特定行或某些行,则必须用行寻址(lineaddressing)。在sed编辑器中有两种形式的行寻址:第一,以数字形式表示区间;第二,用文本模式来过滤出行。两种形式都使用相同的格式来指定地址:

[address] command或者是将特定地址的多个命令分组,如下所示:

1
2
3
4
5
address {
command1
command2
command3
}

sed编辑器会将指定的每条命令作用到匹配指定地址的行上。本节将会演示如何在sed编辑器脚本中使用两种寻址方法。

第一,数字方式的行寻址。

当使用数字方式的行寻址时,可以用行在文本流中的行位置来引用。sed编辑器会将文本流中的第一行编号为1,然后继续按顺序为接下来的行分配行号。在命令中指定的地址可以是单个行号,或是用起始行号、逗号以及结尾行号指定的一定区间范围内的行。这里有个sed命令作用到指定行号的例子,如下所示:

1
2
3
4
5
biotest@ubuntu:~/sed$ sed '2s/dog/cat/' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

sed编辑器只修改地址指定的第二行的文本。这里有另一个例子,这次使用了行地址区间,如下所示:

1
2
3
4
5
biotest@ubuntu:~/sed$ sed '2,3s/dog/cat/' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.

如果需要将命令作用到文本从某行开始的所有行,可以使用美元符号,如下所示:

1
2
3
4
5
biotest@ubuntu:~/sed$ sed '2,$s/dog/cat/' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
第二,使用文本模式过滤器

另一种限制命令作用到哪些行上的方法会稍稍复杂一些。sed编辑器允许指定文本模式来过滤出命令要作用的行。格式为/pattern/command,必须用正斜线将要指定的pattern封起来。sed编辑器会将该命令作用到包含指定文本模式的行上。举个例子,如果你想只修改用户test001的默认shell,可以使用sed命令,如下所示:

1
2
3
biotest@ubuntu:~/sed$ grep biotest /etc/passwd
biotest:x:1000:1000:UBUNTU,,,:/home/biotest:/bin/bash
biotest:x:1000:1000:UBUNTU,,,:/home/biotest:/bin/csh

该命令只作用到匹配文本模式的行上。虽然使用固定文本模式能帮你过滤出特定的值,就跟上面这个用户名的例子一样,但其作用难免有限。sed编辑器在文本模式中支持正则表达式(regular expression)。

第三,命令组合

如果需要在单行上执行多条命令,可以用花括号将多条命令组合在一起,sed编辑器会处理地址行处列出的每条命令,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
biotest@ubuntu:~/sed$ cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
biotest@ubuntu:~/sed$ sed '2{
> s/fox/elephant/
> s/dog/cat/
> }' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown elephant jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

两条命令都会作用到该地址上,也可以在一组命令前指定一个地址区间,如下所示:

1
2
3
4
5
6
7
8
biotest@ubuntu:~/sed$ sed '3,${
> s/brown/green/
> s/lazy/active/
> }' data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick green fox jumps over the active dog.
The quick green fox jumps over the active dog.

删除行

在sed中,删除使用的是d参数,使用此命令需要谨慎,如果用户忘记加入寻址模式的话,流中所有的文都会被删除,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
biotest@ubuntu:~/sed$ cat test6.txt # 原始文本
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
biotest@ubuntu:~/sed$ sed '3d' test6.txt # 删除第3行
This is line number 1.
This is line number 2.
This is line number 4.
biotest@ubuntu:~/sed$ sed '2,3d' test6.txt # 删除第2,3行
This is line number 1.
This is line number 4.
biotest@ubuntu:~/sed$ sed '3,$d' test6.txt # 删除第3行及3行以下
This is line number 1.
This is line number 2.
biotest@ubuntu:~/sed$ sed '/number 1/d' test6.txt # 删除第1行
This is line number 2.
This is line number 3.
This is line number 4.
biotest@ubuntu:~/sed$ sed '/1/,/3/d' test6.txt # 删除1到3行
This is line number 4.
# 指定的第一个模式会“打开”行删除功能,第二个模式会“关闭”行删除功能。sed编辑器会删除两个指定行之间的所有行

只要sed编辑器在数据流中匹配到了开始模式,删除功能就会打开。这可能会导致意外的结果,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
biotest@ubuntu:~/sed$ cat test7.txt
This is line number 1.
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 1 again.
THis is text you want to keep.
This is the last line in the file.
biotest@ubuntu:~/sed$ sed '/1/,/3/d' test7.txt
This is line number 4.

在这段代码中,第二个出现数字“1”的行再次触发了删除命令,因为没有找到停止模式,所以就将数据流中的剩余行全部删除了。如果你指定了一个从未在文本中出现的停止模式,显然会出现另外一个问题,如下所示:

1
2
biotest@ubuntu:~/sed$ sed '/1/,/5/d' test7.txt
biotest@ubuntu:~/sed$

在这段代码中,因为删除功能在匹配到第一个模式的时候打开了,但一直没匹配到结束模式,所以整个数据流都被删掉了。

插入和附加文本

sed编辑器可以向数据流插入和附加文本行。它有两种操作:第一,插入(insert):命令(i)会在指定行前增加一个新行;第二,追加(append)命令(a)会在指定行后增加一个新行。这两条命令在使用格式上有些特殊,它们不能在单个命令行上使用。用户必须指定是要将行插入还是附加到另一行。格式如下:

1
2
sed '[address] command\
new line'

其中,new line中的文本将会出现在sed编辑器输出中指定的位置。记住,当使用插入命令时,文本会出现在数据流文本的前面,如下所示:

1
2
3
4
5
6
7
biotest@ubuntu:~/sed$ echo "Test Line2"|sed 'i\Test Line 1' # 在第2行之前插入文本
Test Line 1
Test Line2
biotest@ubuntu:~/sed$ echo "Test Line2"|sed 'a\Test Line 1' # 在第2行之后插入文本
Test Line2
Test Line 1

在命令行界面提示符上使用sed编辑器时,用户会看到次提示符来提醒输入新的行数据。用户必须在该行完成sed编辑器命令。一旦你输入了结尾的单引号,bash shell就会执行该命令,如下所示:

1
2
3
4
biotest@ubuntu:~/sed$ echo "Test Line 2"|sed 'i\
> Test Line 1'
Test Line 1
Test Line 2

前面所述的方法只能给数据流中的文本前面或后面添加文本,但有时候还需要向一行的前后添加文本,要向数据流行内部插入或附加数据,必须用寻址来告诉sed编辑器想让数据出现在什么位置。可以在用这些命令时只指定一个行地址。可以匹配一个数字行号或文本模式,但不能用地址区间(因为用户只能将文本插入或附加到单个行的前面或后面,而不是行区间的前面或后面),在下面的例子中,用户将一个新行插入到数据流第三行前。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
biotest@ubuntu:~/sed$ cat test6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
# 将一个新行插入到数据流第三行前
biotest@ubuntu:~/sed$ sed '3i\
> This is an inserted line.' test6.txt
This is line number 1.
This is line number 2.
This is an inserted line.
This is line number 3.
This is line number 4.
#将一个新行附加到数据流中第三行后
biotest@ubuntu:~/sed$ sed '3a\
> This is an appended line.' test6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an appended line.
This is line number 4.

如果有一个多行数据流,需要将新行追加到数据流的末决,只要用代表数据最后一行的美元符号即可,如下所示:

1
2
3
4
5
6
7
biotest@ubuntu:~/sed$ sed '$a\
> This is a new line of text.' test6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is a new line of text.

同样的方法也适用于要在数据流起始位置增加一个新行。只要在第一行之前插入新行即可。要插入或附加多行文本,就必须对要插入或附加的新文本中的每一行使用反斜线,直到最后一行。

1
2
3
4
5
6
7
8
9
biotest@ubuntu:~/sed$ sed '1i\
> This ia one line of new text.\
> This is another line of new text.' test6.txt
This ia one line of new text.
This is another line of new text.
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

修改行

sed可以对文本进行修改,使用的参数是c,意思为修改(change)。这个参数允许修改数据流中整行文本的内容。它跟插入和附加命令的工作机制一样,必须在sed命令中单独指定新行,如下所示:

1
2
3
4
5
6
biotest@ubuntu:~/sed$ sed '3c\
> This is a change line of text.' test6.txt
This is line number 1.
This is line number 2.
This is a change line of text.
This is line number 4.

在这个例子中,sed编辑器会修改第三行中的文本。也可以用文本模式来寻址,如下所示:

1
2
3
4
5
6
biotest@ubuntu:~/sed$ sed '/number 3/c\
> This is a changed line of text.' test6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.

文本模式修改命令会修改它匹配的数据流中的任意文本行,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
biotest@ubuntu:~/sed$ cat test8.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 1 again.
This is yet another line.
This is the last line in the file.
biotest@ubuntu:~/sed$ sed '/number 1/c\
> This is a changed line of text.' test8.txt
This is a changed line of text.
This is line number 2.
This is line number 3.
This is line number 4.
This is a changed line of text.
This is yet another line.
This is the last line in the file.

在修改命令中使用地址区间,结果就会出问题,如下所示:

1
2
3
4
5
biotest@ubuntu:~/sed$ sed '2,3c\
> This is a new line of text.' test6.txt
This is line number 1.
This is a new line of text.
This is line number 4.

sed编辑器会用这一行文本来替换数据流中的两行文本,而不是逐一修改这两行文本。

转换命令

sed中还有一个转换命令,参数是y,转换( transform )可以处理单个字符。

1
[address]y/inchars/outchars/

转换命令会对inchars和outchars值进行一对一的映射。inchars中的第一个字符会被转换为outchars中的第一个字符,第二个字符会被转换成outchars中的第二个字符。这个映射过程会一直持续到处理完指定字符。如果inchars和outchars的长度不同,则sed编辑器会产生一条错误消息,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
biotest@ubuntu:~/sed$ cat test8.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 1 again.
This is yet another line.
This is the last line in the file.
biotest@ubuntu:~/sed$ sed 'y/123/789/' test8.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
This is line number 7 again.
This is yet another line.
This is the last line in the file.

在这个案例中,inchars械中指定的字符的每个实例都会被替换成outchars模式中相同位置的那个字符,转换命令是一个全局命令,它会在文本行中找到所有指定字符自动进行转换,而不会考虑它们出现的位置,如下所示:

1
2
biotest@ubuntu:~/sed$ echo "This 1 is a test of 1 try."|sed 'y/123/456/'
This 4 is a test of 4 try.

sed编辑器转换了在文本行中匹配到的字符1的两个实例。你无法限定只转换在特定地方出现的字符。

回顾打印

有3个命令可以打印文本行,分别为:

  1. p命令用来打印文本行;
  2. 等号(=)用于打印行号;
  3. l(小写的L)用于列出行。

打印行

这里的p参业与替换中的p参数类似,它可以打印sed编译器输出中的一行,如果只使用这个命令,就是显示这一行命令,如下所示:

1
2
3
biotest@ubuntu:~/sed$ echo "This is a test"|sed 'p'
This is a test
This is a test

p参数就是打印已有的数据文本,打印命令常用的用法就是打印包含匹配文本模式的行,如下所示:

1
2
3
4
5
6
7
biotest@ubuntu:~/sed$ cat test6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
biotest@ubuntu:~/sed$ sed -n '/number 3/p' test6.txt
This is line number 3.

在命令行上用-n选项,你可以禁止输出其他行,只打印包含匹配文本模式的行。也可以用它来快速打印数据流中的某些行,如下所示:

1
2
3
biotest@ubuntu:~/sed$ sed -n '2,3p' test6.txt
This is line number 2.
This is line number 3.

如果需要在修改之前查看行,也可以使用打印命令,比如与替换或修改命令一起使用。可以创建一个脚本在修改行之前显示该行,如下所示:

1
2
3
4
5
6
biotest@ubuntu:~/sed$ sed -n '/3/{
> p
> s/line/test/p
> }' test6.txt
This is line number 3.
This is test number 3.

sed编辑器命令会查找包含数字3的行,然后执行两条命令。首先,脚本用 p 命令来打印出原始行;然后它用 s 命令替换文本,并用 p 标记打印出替换结果。输出同时显示了原来的行文本和新的行文本。

打印行号

等号命令会打印行在数据流中的当前行号。行号由数据流中的换行符决定。每次数据流中出现一个换行符,sed编辑器会认为一行文本结束了,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
biotest@ubuntu:~/sed$ cat data1.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
biotest@ubuntu:~/sed$ sed '=' data1.txt
1
The quick brown fox jumps over the lazy dog.
2
The quick brown fox jumps over the lazy dog.
3
The quick brown fox jumps over the lazy dog.
4
The quick brown fox jumps over the lazy dog.

sed编辑器在实际的文本行出现前打印了行号。如果你要在数据流中查找特定文本模式的话,等号命令用起来非常方便,如下所示:

1
2
3
4
5
6
biotest@ubuntu:~/sed$ sed -n '/number 4/{
> =
> p
> }' test6.txt
4
This is line number 4.

利用 -n 选项,你就能让sed编辑器只显示包含匹配文本模式的行的行号和文本。

列出行

列出(list)命令(l)可以打印数据流中的文本和不可打印的ASCII字符。任何不可打印字符要么在其八进制值前加一个反斜线,要么使用标准C风格的命名法(用于常见的不可打印字符),比如\t,来代表制表符。

1
2
3
4
biotest@ubuntu:~/sed$ cat data9.txt
This line contains tabs.
biotest@ubuntu:~/sed$ sed -n 'l' data9.txt
This\tline\tcontains\ttabs.$

制表符的位置使用\t来显示。行尾的美元符表示换行符。

使用sed处理文件

写入文件

w选项用于向文件写入行,该选项的命令格式为[address] w filename,其中filename可以使用相对路径或绝对路径,并且用户要有文件的写权限,地址可以是sed中支持的任意类型的寻址方式,例如单个行号、文本模式、行区间或文本模式。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
biotest@ubuntu:~/sed$ sed '1,2w test.txt' test6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
biotest@ubuntu:~/sed$ cat test6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
biotest@ubuntu:~/sed$ cat test.txt
This is line number 1.
This is line number 2.

如果不想让行显示到STDOUT上,可以用sed命令的-n选项。如果要根据一些公用的文本值从主文件中创建一份数据文件,比如下面的邮件列表中的,那么w命令会非常好用,如下所示:

1
2
3
4
5
6
7
8
biotest@ubuntu:~/sed$ cat data11.txt
Zhang San
Li Si
Wang Er
Liu Qi
biotest@ubuntu:~/sed$ sed -n '/Zhang/w Zhang.txt' data11.txt
biotest@ubuntu:~/sed$ cat Zhang.txt
Zhang San

sed编辑器会只将包含文本模式的数据写入目标文件。

从文件读取数据

取(read)命令(r)允许用户将一个独立文件中的数据插入到数据流中。读取命令的格式为[address]r filename,其中filename参数指定了数据文件的绝对路径或相对路径,在读取命令中使用地址区间,只能指定单独一个行号或文本模式地址。sed编辑器会将文件中的文本插入到指定地址后,如下所示:

1
2
3
4
5
6
7
8
9
10
biotest@ubuntu:~/sed$ cat data12.txt
This is an added line.
This is the second added line.
biotest@ubuntu:~/sed$ sed '3r data12.txt' test6.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.

使用文本模式进行读取,如下所示:

1
2
3
4
5
6
7
biotest@ubuntu:~/sed$ sed '/number 2/r data12.txt' test6.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.

如果需要在数据流的末尾添加文件,需要使用美元符号,如下所示:

1
2
3
4
5
6
7
biotest@ubuntu:~/sed$ sed '$r data12.txt' test6.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.

读取命令的另一个很有用的用法是和删除命令配合使用:利用另一个文件中的数据来替换文件中的占位文本。举例来说,假定你有一份套用信件保存在文本文件中,如下所示:

1
2
3
4
biotest@ubuntu:~/sed$ cat notice.std
Would the following people:
LIST
please report to the ship's captain.

套用信件将通用占位文本 LIST 放在人物名单的位置。要在占位文本后插入名单,只需读取 命令就行了。但这样的话,占位文本仍然会留在输出中。要删除占位文本的话,你可以用删除命 令。结果如下:

1
2
3
4
5
6
7
8
9
10
biotest@ubuntu:~/sed$ sed '/LIST/{
> r data11.txt
> d
> }' notice.std
Would the following people:
Zhang San
Li Si
Wang Er
Liu Qi
please report to the ship's captain.

现在占位文本已经被替换成了数据文件中的名单。