转自:http://blog.csdn.net/showman/archive/2009/07/31/4396142.aspx
1. 打印:p
[root@TestAs4 chap04]# cat datafile 原文件
northwest NW Charles Main 3.0 .98 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 Hemenway 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
[root@TestAs4 chap04]# sed-n '/north/p' datafile 取消默认输出 只打印包含模板的行
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
[root@TestAs4 chap04]# sed '/north/p' datafile 打印包含模板的行及打印默认输出
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 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 Hemenway 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
2. 删除:d
[root@TestAs4 chap04]# sed '3d'datafile 删除第三行
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 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
[root@TestAs4 chap04]# sed '3,$d'datafile 删除第三行到最后的所有行
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
[root@TestAs4 chap04]# sed '/north/d' datafile 删除所有包含模板north的行
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 Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
central CT Ann Stephens 5.7 .94 5 13
3. 选定行的范围:逗号
[root@TestAs4 chap04]# sed -n '/west/,/east/p' datafile 所有在模板west和east所确定的行都被打印
northwest NW Charles Main 3.0 .98 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 Hemenway 4.0 .7 4 17
[root@TestAs4 chap04]# sed -n '1,5'p datafile 打印第一、五行的内容
northwest NW Charles Main 3.0 .98 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 Hemenway 4.0 .7 4 17
[root@TestAs4 chap04]# sed'/west/,/east/s/$/**?VACA**/' datafile 对于east和west之间的行,末尾用**?VACA**替换
northwest NW Charles Main 3.0 .98 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 Hemenway 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
[root@TestAs4 chap04]# sed-n '/west/,/south/p' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
4.多点编辑:e命令
[root@TestAs4 chap04]# sed -e '1,3d' -e 's/Hemenway/Jones/' datafile 删除1到3行,用Hemenway替换Jones
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Jones4.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
5. 从文件读入:r 命令
[root@TestAs4 chap04]# cat newfile
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
| ***SUAN HAS LEFT THE COMPANY*** |
|_________________________________|
[root@TestAs4 chap04]# sed'/Suan/r newfile'datafile 把newfile文件内容放到Suan行的下面
northwest NW Charles Main 3.0 .98 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
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
| ***SUAN HAS LEFT THE COMPANY*** |
|_________________________________|
southeast SE Patricia Hemenway 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
注:如果不止一个Suan 则newfile的内容就将显示在所有匹配行的下面
6. 写入文件:w命令
[root@TestAs4 chap04]# sed-n '/north/wnewfile2'datafile 命令w表示把所有包含north的行写入到newfile2
[root@TestAs4 chap04]# cat newfile2
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
7. 追加:a 命令
[root@TestAs4 chap04]#sed '/^north/a ---->THE NORTH SALES DISTRICT HAS MOVED
northwest NW Charles Main 3.0 .98 3 34
---->THE NORTH SALES DISTRICT HAS MOVED
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 Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
---->THE NORTH SALES DISTRICT HAS MOVED
north NO Margot Weber 4.5 .89 5 9
---->THE NORTH SALES DISTRICT HAS MOVED
central CT Ann Stephens 5.7 .94 5 13
注: 在出现首个单词是north的后一行追加---->THE NORTH SALES DISTRICT HAS MOVED行
8. 插入: i 命令
[root@TestAs4 chap04]# sed '/eastern/i\
> NEW ENGLAND REGION\
> -------------------------------------' datafile
northwest NW Charles Main 3.0 .98 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 Hemenway 4.0 .7 4 17
NEW ENGLAND REGION
-------------------------------------
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
注:如果模板eastern被匹配,i命令把反斜杠后面的文本插入到包含eastern的行的前面
9. 替换:s 命令
[root@TestAs4 oracle]# pwd
/u01/app/oracle
[root@TestAs4 oracle]# pwd| sed's/\/[^\/]*$/old/' 把“/ u01/app/oracle”的 “/oracle”替换为old
/u01/appold
[root@TestAs4 chap04]# sed -n 's/Hemenway/Jones/pg' datafile 所有的Hemenway行被Jones 替换并打印
southeast SE Patricia Jones4.0 .7 4 17
分享到:
相关推荐
sed 用法sed 用法sed 用法sed 用法sed 用法
根据个人平时多年的学习积累,总结的sed所有用法,包括sed中删除,修改,添加,插入,合并,交换,读入,替换,跳转,标签,转换等高级用法!
sed中文指南 详细介绍了SED的用法与实例
此文档包含了对sed的从用法介绍到用法实例,应该会对大家有所帮忙加深对sed的理解
LINUX的awk和sed的常用用法 LINUX的awk和sed的常用用法
通用线程sed 实例大家可以看看啦,不错的!
sed,awk用法,sed,awk是linux开发必备知识,公司常用技术,很好很强大的sed和awk用法介绍,绝对值得您收藏学习。好资源大家一起分享!
本文用一个个简短的例子展示sed每一个命令的用法,简单易懂,希望对你有些帮助。 网上大多资料都是英文版的,好多人看着不打舒服,因此写此题集,助你快速提升对sed的驾驭能力。
sed命令_Linux sed 命令用法详解:功能强大的流式文本编辑器
shell sed study example
sed高级用法.html 这个可要好好学一学,用得着,十分方便的工具
详细讲解了sed流编辑器的用法(包括sed的各种参数以及sed脚本文件的编写)。
文章主要介绍Linux下sed命令的用法,感兴趣的朋友可以看看。
sed用法介绍,高阶应用
sed和awk使用的几个经典实例,都是比较典型的,可能经常会用到
想学好sed和awk吗, 被别人复杂的sed和awk神奇和高效惊叹了吗,想知道其中的原因吗, 这里我搜集了讲解sed和awk全面的资料,让你自如的完成想要的操作,字符串的操作没有你办不到
有不错的例子,对sed有更好的理解,只有多做练习,才能提高,才能更好的理解原理
5.4: 正则表达式 、 sed基本用法 、 sed文本块处理.docx
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作,下面先了解一下sed的用法
在处理庞大数据量时,善用Linux三剑客sed grep awk能省下不少劲。文档例子仅供参考学习!