Sed是一个编辑器,可能有人会奇怪,不是命令吗?实际上它是一个非交互式,它的功能可以达到VI、EX的大部分功能。有些人可能会不信,不过你学习一下,就会发现它功能确实强大,它不等于Grep,Grep只能显示文件内容,不能编辑文件,但sed可以。使用sed在一些脚本中动态修改某些文件非常有用,不需要使用CAT,然后再写。常用的LINUX SED版本是FSF的自由软件。
[windriver@windriver-machine ltest]$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
[windriver@windriver-machine ltest]$
Sed的工作方式如下图:
从这个图中可以看出SED是一行一行的读入到缓冲区,然后对缓冲区进行操作,这些操作就是SED的选项,经过这些操作之后,显示到屏幕或者写回到文件中。
通过前面的正则表达式学习,我们进行SED学习就非常简单,SED里面的正则表达式是相同的。SED不同于grep之外在于SED是一行一行处理文件的,它可以对行号进行定位,同时它除了一些选项参数之外,它有一些自己特定的操作,就是命令。 因此,对SED,我们需要记住的两件事,就是它有Address和COMMAND 两大特性。
sed的一些常用命令格式如下表所示:
命令 | 功能 |
a\ | 在当前行后添加一行或多行 |
c\ | 用新文件替换当前行 |
d | 删除当前行 |
i\ | 在当前行之前插入一行 |
h | 把模式空间的内容复制到缓冲区中 |
H | 把模式空间的内容追加到缓冲区中 |
g | 取出缓冲区中的内容 ,拷到模式空间中,是替换最后一行 |
G | 取出缓冲区中的内容 ,追加到模式空间中的最后一行 |
l | 列出所有非打印字符 |
p | 打印行 |
n | 读取下一个输入行,并且用新的命令来处理这个新行 |
q | 退出 |
! | 将命令应用到非指定行 |
s | 读取行,并将一个字符串替换成另一个字符串。 |
r | 读入新文件内容 |
sed针对s命令,还要附带定些标签。如下表所示:
g | 在一个行中全局替换 |
p | 打印行 |
w | 将行写到一个文件中 |
x | 将缓冲区与模式空间中内容交换 |
y | 在不用正则表达式时,将一个字符转成另外一个字符 |
Sed 还有一些选项标记如下:
选项 | 功能 |
-e | 允许多行编辑 |
-f | 后面跟sed脚本文件 |
-n | 取消缺省输出 |
-i | 编辑修改后直接作用到原文件 |
[windriver@windriver-machine ltest]$ sed '1,3d' datafile
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed '/SO/d' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed '/SO/!d' datafile
southern SO Suan chin 5.1 .95 4 15
[windriver@windriver-machine ltest]$
sed对文件的修改方式,通常SED编辑的是缓冲区内容,然后将编辑后的结果显示在屏幕上,因此对原文件不破坏,因此,如果要真正写回到原文件,需要重定向到一个新文件,然后拷贝回去。
[windriver@windriver-machine ltest]$ sed '1,3d' datafile >temp
[windriver@windriver-machine ltest]$ cat temp
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed -i '1,3d' temp
[windriver@windriver-machine ltest]$ cat temp
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$
当然SED可以使用选项-i进行in place修改。
SED操作的退出状态,成功是0,一般的失败是1,如果有报错信息的,通常是需要根据错误信息进行语法修改。
[windriver@windriver-machine ltest]$ sed '1,3d' datafile
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ echo $?
0
[windriver@windriver-machine ltest]$ sed '1,3f' datafile
sed: -e expression #1, char 4: unknown command: `f'
[windriver@windriver-machine ltest]$ echo $?
1
[windriver@windriver-machine ltest]$
SED正则表达式,同GREP类似,GREP缺省的元字符,SED都支持,但SED还支持一些特殊的元字符,如下表所示:
& | 保存查找字符串,这样在后面可以对这个字符串进行替换(这个有点类似于前面\(\) \r | s/love/**&**/ | love这个字符串被**love**字符串替换 |
[windriver@windriver-machine ltest]$ sed 's/CT/**&**/' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central **CT** Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed 's/north/&ing/g' datafile
northingwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northingeast NE AM Main Jr. 5.1 .94 3 13
northing NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$
[windriver@windriver-machine ltest]$ sed -n '/north/p' datafile
northwest NW Charles Main 3.0 .90 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
[windriver@windriver-machine ltest]$ sed '/north/p' datafile
northwest NW Charles Main 3.0 .90 3 34
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed -n '/north/p' datafile
northwest NW Charles Main 3.0 .90 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
[windriver@windriver-machine ltest]$ sed '/north/' datafile
sed: -e expression #1, char 7: missing command
[windriver@windriver-machine ltest]$ sed -n '/north/' datafile
sed: -e expression #1, char 7: missing command
[windriver@windriver-machine ltest]$ sed '3d' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed '3,$d' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
[windriver@windriver-machine ltest]$ sed '$d' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
[windriver@windriver-machine ltest]$ sed '/north/d' datafile
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed 's/[0-9][0-9]$/&.5/p' datafile
northwest NW Charles Main 3.0 .90 3 34.5
northwest NW Charles Main 3.0 .90 3 34.5
western WE Sharon Gray 5.3 .97 5 23.5
western WE Sharon Gray 5.3 .97 5 23.5
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15.5
southern SO Suan chin 5.1 .95 4 15.5
southeast SE Patricia 4.0 .7 4 17.5
southeast SE Patricia 4.0 .7 4 17.5
eastern EA TB Savage 4.4 .84 5 20.5
eastern EA TB Savage 4.4 .84 5 20.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13.5
central CT Ann Stephens 5.7 .94 5 13.5
[windriver@windriver-machine ltest]$ sed 's/[0-9][0-9]$/&.5/g' datafile
northwest NW Charles Main 3.0 .90 3 34.5
western WE Sharon Gray 5.3 .97 5 23.5
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15.5
southeast SE Patricia 4.0 .7 4 17.5
eastern EA TB Savage 4.4 .84 5 20.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13.5
[windriver@windriver-machine ltest]$ sed 's/[0-9][0-9]$/&.5/' datafile
northwest NW Charles Main 3.0 .90 3 34.5
western WE Sharon Gray 5.3 .97 5 23.5
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15.5
southeast SE Patricia 4.0 .7 4 17.5
eastern EA TB Savage 4.4 .84 5 20.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13.5
[windriver@windriver-machine ltest]$ sed 's/[0-9][0-9]$/&.5/g' datafile
northwest NW Charles Main 3.0 .90 3 34.5
western WE Sharon Gray 5.3 .97 5 23.5
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15.5
southeast SE Patricia 4.0 .7 4 17.5
eastern EA TB Savage 4.4 .84 5 20.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13.5
[windriver@windriver-machine ltest]$ sed 's/[0-9][0-9]$/&.5/gp' datafile
northwest NW Charles Main 3.0 .90 3 34.5
northwest NW Charles Main 3.0 .90 3 34.5
western WE Sharon Gray 5.3 .97 5 23.5
western WE Sharon Gray 5.3 .97 5 23.5
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15.5
southern SO Suan chin 5.1 .95 4 15.5
southeast SE Patricia 4.0 .7 4 17.5
southeast SE Patricia 4.0 .7 4 17.5
eastern EA TB Savage 4.4 .84 5 20.5
eastern EA TB Savage 4.4 .84 5 20.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13.5
central CT Ann Stephens 5.7 .94 5 13.5
[windriver@windriver-machine ltest]$ sed -n 's/[0-9][0-9]$/&.5/gp' datafile
northwest NW Charles Main 3.0 .90 3 34.5
western WE Sharon Gray 5.3 .97 5 23.5
southern SO Suan chin 5.1 .95 4 15.5
southeast SE Patricia 4.0 .7 4 17.5
eastern EA TB Savage 4.4 .84 5 20.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
central CT Ann Stephens 5.7 .94 5 13.5
[windriver@windriver-machine ltest]$
SED中间的正则表达式的分隔符/可以用#代替。结果是一样的。当然这个只适用于s命令,任意字符实际上都可以,但\和\n之外。
[windriver@windriver-machine ltest]$ sed 's#3#88#g' datafile
northwest NW Charles Main 88.0 .90 88 884
western WE Sharon Gray 5.88 .97 5 288
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 88 188
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 188
[windriver@windriver-machine ltest]$ sed 's030880g' datafile
northwest NW Charles Main 88.0 .90 88 884
western WE Sharon Gray 5.88 .97 5 288
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 88 188
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 188
[windriver@windriver-machine ltest]$
SED的Addressing除了直接指定如1,3之个,也可以用正则表达式。
[windriver@windriver-machine ltest]$ sed -n '/west/,/east/p' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
[windriver@windriver-machine ltest]$ sed -n '5,/^northeast/p' datafile
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
[windriver@windriver-machine ltest]$ sed '/west/,/east/s/$/**VACA**/' datafile
northwest NW Charles Main 3.0 .90 3 34**VACA**
western WE Sharon Gray 5.3 .97 5 23**VACA**
southwest SW Lewis Dalsass 2.7 .8 2 18 **VACA**
southern SO Suan chin 5.1 .95 4 15**VACA**
southeast SE Patricia 4.0 .7 4 17**VACA**
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$
SED其它的选项如-e 是多项编辑、r读取新文件
[windriver@windriver-machine ltest]$ sed -e '1,3d' -e 's/CT/BBK/' datafile
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central BBK Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed -e '1,3d' -e 's/CT/BBC/' datafile
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central BBC Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ cat newfile
welcome sed tutorial
[windriver@windriver-machine ltest]$ sed '/Suan/r newfile' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
welcome sed tutorial
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$ sed -n '/north/w 123' datafile
[windriver@windriver-machine ltest]$ cat 123
northwest NW Charles Main 3.0 .90 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
[windriver@windriver-machine ltest]$ sed -n '/north/p' datafile >456
[windriver@windriver-machine ltest]$ cat 456
northwest NW Charles Main 3.0 .90 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
[windriver@windriver-machine ltest]$ sed '/^north/a\--->sina.com<---' datafile
northwest NW Charles Main 3.0 .90 3 34
--->sina.com<---
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
--->sina.com<---
north NO Margot Weber 4.5 .89 5 9
--->sina.com<---
central CT Ann Stephens 5.7 .94 5 13
SED a命令是插入命令,是在当前的行之后追加后面的内容,
[windriver@windriver-machine ltest]$ sed '/^north /a\--->sina.com<---' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
--->sina.com<---
central CT Ann Stephens 5.7 .94 5 13
SED i命令是插入命令,是在当前的行之前插入后面的内容,
[windriver@windriver-machine ltest]$ sed '/^north /i\--->sina.com<---' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
--->sina.com<---
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
SED c命令是修改命令,是将当前的行行修改成后面的内容,不是替换
[windriver@windriver-machine ltest]$ sed '/eastern/c\--->sina.com<---' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
--->sina.com<---
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
SED n命令是下行命令,是将当前匹配行,如eastern EA…这行的下一行northeast 执行替换命令。
[windriver@windriver-machine ltest]$ sed '/eastern/{ n;s/AM/AAA/; }' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AAA Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
SED y命令是转换命令,类似于LINUX下的TR命令。将就是将所有的字符转换成对应的字符,必须字符一一对应。
[windriver@windriver-machine ltest]$ sed '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' datafile
NORTHWEST NW CHARLES MAIN 3.0 .90 3 34
WESTERN WE SHARON GRAY 5.3 .97 5 23
SOUTHWEST SW LEWIS DALSASS 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
[windriver@windriver-machine ltest]$
sed q命令是退出命令
[windriver@windriver-machine ltest]$ sed '5q' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
[windriver@windriver-machine ltest]$ sed '5' datafile
sed: -e expression #1, char 1: missing command
[windriver@windriver-machine ltest]$ sed '/Lewis/{ s/Lewis/BBC/;q;}' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW BBC Dalsass 2.7 .8 2 18
[windriver@windriver-machine ltest]$
SED h通过-e 选项,结合g和G来使用,进行前面找到的暂存,并取出加到后面来。
[windriver@windriver-machine ltest]$ sed -e '/northeast/h' -e '$G' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
northeast NE AM Main Jr. 5.1 .94 3 13
SED h将前面查询出来的行暂存起来,
[windriver@windriver-machine ltest]$ sed -e '/northeast/h' -e '$g' datafile
northwest NW Charles Main 3.0 .90 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
northeast NE AM Main Jr. 5.1 .94 3 13
[windriver@windriver-machine ltest]$ sed -e '/WE/{h;d; }' -e '/CT/{G; }' datafile
northwest NW Charles Main 3.0 .90 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan chin 5.1 .95 4 15
southeast SE Patricia 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
western WE Sharon Gray 5.3 .97 5 23