转自: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 Jones  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

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/w  newfile2'  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 Jones  4.0     .7      4       17

实例讲解sed的9种常见用法相关推荐

  1. 动词ing形式的5种用法_动词ing的几种常见用法

    龙源期刊网 http://www.qikan.com.cn 动词 ing 的几种常见用法 作者:张爱娟 来源:<中学生英语 · 阅读与写作> 2016 年第 10 期 为了让同学们更好掌握 ...

  2. Java接口的几种常见用法

    接口(interface)对于面向对象编程来说是一个非常重要的概念.它是一系列方法的声明,却没有具体实现.有些编程语言,比如swift,把接口解释成"协议(protocol)",我 ...

  3. 计算机excelsumif的公式,excel中sumif函数的几种常见用法

    在excel中sumif函数是一个非常有用的函数,它可以按条件进行求和.其实从这个函数的名字就可以看出来它是用来干什么的,SUM是求和,IF是如果.如果什么..就求和,其实就是按条件求和.本教程图文详 ...

  4. 二极管在LDO电路中的几种常见用法

    文章目录 1.防反接--二极管接在VIN端 2.防反接--二极管接在GND端 3.输入输出钳位保护 4.降压&防灌电 大家好,我是记得诚. 交流群的一次讨论,想到简单汇总一下,二极管在LDO电 ...

  5. ROS MarkerArray的几种常见用法

    ros使用过程中,经常会用到rviz可视化各种数据.而在rviz中,marker与markerarray是常用的一种可视化工具,最近也用到过几次了,这里随手记录一下. 1.makerarray画线 在 ...

  6. linux中touch命令如何使用,Linux Touch命令的8种常见用法

    除了在Linux上简单地创建一个空文件之外,Linux touch命令还有更多的用途.您也可以使用它来更改现有文件的时间戳,包括它们的访问和修改时间.本文介绍了8种通过Linux终端使用touch命令 ...

  7. 消息模式Toast.makeText的几种常见用法

    转载自:http://daikainan.iteye.com/blog/1405575 Toast 是一个 View 视图,快速的为用户显示少量的信息. Toast 在应用程序上浮动显示信息给用户,它 ...

  8. java console 交互_实例讲解java中Console类的用法

    java的Console类的使用方法及实例 java的Console类的使用方法及实例 JDK 6中提供了java.io.Console类专用来访问基于字符的控制台设备.如果你的Java程序要与Win ...

  9. 以视频爬取实例讲解Python爬虫神器Beautiful Soup用法

    1.安装BeautifulSoup4 easy_install安装方式,easy_install需要提前安装 1 easy_install beautifulsoup4 pip安装方式,pip也需要提 ...

最新文章

  1. playbook核心元素之 模板template 介绍(7)
  2. 石川es6课程---4、箭头函数
  3. 【Python学习系列二十七】pearson相关系数计算
  4. Citrix VDI实战攻略之八:测试验收
  5. SAP实施后你的关键用户还关键吗?
  6. Luogu P4708 画画 (Burnside引理、组合计数)
  7. LightGBM笔记
  8. python 网页爬取数据生成文字云图
  9. maya室内模型_C4d和3dmax、maya相比有什么优势?
  10. vue function (i)第一次点击不执行_vue下$nextTick及原理浅析
  11. SQL Server监控全解析
  12. 为啥vb被计算机二级取消,计算机二级vb考试是不是今年最后一年
  13. c语言怎么设置命令行字体大小,C语言入门教程-命令行参数
  14. SQLiteDatabaseLockedException: database is locked
  15. 广告行业中静态创意和动态创意区别
  16. 你的伙伴对你最大会话_甜炸了的表白套路对话 套路深到你无法拒绝
  17. Java struts mysql实现的薪资工资管理系统源码+运行教程
  18. 巴法络nas硬盘挂linux,教你轻松DIY——巴法络家用NAS使用详解
  19. 使用VS2012进行单元测试
  20. 服务器和电脑主机的区别

热门文章

  1. oracle linux vs centos我们选择谁
  2. Vista及Win7常见故障(拷贝)
  3. Elasticearch 安装 基础介绍 (一)
  4. OpenCV 掩膜的应用
  5. java安全点_关于OopMap、SafePoint(安全点)以及安全区域
  6. backtrace java_在c file中打出backtrace到某个文件中
  7. myeclipse 2019中文版
  8. Flask 自定义过滤器多个参数传入
  9. 2017 《Java技术预备作业》
  10. 第二章(jQuery选择器)