sed命令选项及作用

-n  不打印所有的行到标准输出

-e  表示将下一个字符串解析为sed编辑命令

-f  表示正在调用sed脚本文件

 sed编辑命令

p  打印匹配行

=  打印文件行号

a\  在定位行号之后追加文本信息

i\  在定位行号之前追加文本信息

d  删除定位行

c\  用新文本替换定位文本

r  从另外一个文件中读文本

w  将文本写入到一个文件

y  变换字符

q  第一个模式匹配玩成后退出

{}  在定位行执行的命令组

n  读取下一个输入行,用下一个命令处理新的行

 

接下来以下面的例子来练习sed命令

[root@git ~]# cat input
This is a Certificate Request file:

It should be mailed to zawu@seu.edu.cn

====================================================
Certificate Subject:

/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus
The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.
/home/alloy/linuxshell/CH02/usercert.pem

<1>看一下-n的用途

[root@git ~]# sed -n '1p' input                         //加上-n选项只打印第一行
This is a Certificate Request file:
[root@git ~]# sed '1p' input                            //不加-n选项会输出第一行后再输出全文
This is a Certificate Request file:
This is a Certificate Request file:

It should be mailed to zawu@seu.edu.cn

====================================================
Certificate Subject:

/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus
The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.
/home/alloy/linuxshell/CH02/usercert.pem

<2>我想打印出文件的3~6行

[root@git ~]# sed -n '3,6p' input
It should be mailed to zawu@seu.edu.cn

====================================================
Certificate Subject:

<3>打印匹配certificate关键字的行

[root@git ~]# sed -n '/certificate/p' input
The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.

<4>打印匹配Certificate关键字的行号及内容(两种方法)

[root@git ~]# sed -n -e '/Certificate/=' -e '/Certificate/p' input
1
This is a Certificate Request file:
6
Certificate Subject:

[root@git ~]# sed -n '/Certificate/{=;p}' input
1
This is a Certificate Request file:
6
Certificate Subject:

<5>在与关键字"file:"相匹配的行后追加内容"We append a new line."

[root@git ~]# sed '/file:/a\We append a new line.' input
This is a Certificate Request file:
We append a new line.

It should be mailed to zawu@seu.edu.cn

====================================================
Certificate Subject:

/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus
The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.
/home/alloy/linuxshell/CH02/usercert.pem

<6>匹配包含以bus结尾字符串的行

[root@git ~]# sed -n '/.*bus/p' input
/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

<7>打印不在2~10之间的行

[root@git ~]# sed -n '2,10!p' input
This is a Certificate Request file:
To install this user certificate,please save this e-mail message into the following file.

/home/alloy/linuxshell/CH02/usercert.pem

<8>打印与seugrid的匹配行到最后一行的内容

[root@git ~]# sed -n '/seugrid/,$p' input
/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.

/home/alloy/linuxshell/CH02/usercert.pem

<9>在匹配"file:"字符前插入"We insert a new line."

[root@git ~]# sed '/file:/i\We insert a new line.' input
We insert a new line.
This is a Certificate Request file:

It should be mailed to zawu@seu.edu.cn

====================================================
Certificate Subject:

/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.

/home/alloy/linuxshell/CH02/usercert.pem

<10>把匹配"file:"的行修改为"We modify this line."

[root@git ~]# sed '/file:/c\We insert a new line.' input
We insert a new line.

It should be mailed to zawu@seu.edu.cn

====================================================
Certificate Subject:

/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.

/home/alloy/linuxshell/CH02/usercert.pem

<11>删除文本的第一行和最后一行

[root@git ~]# sed -e '1d' -e '$d' input

It should be mailed to zawu@seu.edu.cn

====================================================
Certificate Subject:

/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.

<12>把文本中Certificate替换成CERTIFICATE

[root@git ~]# sed -n 's/Certificate/CERTIFICATE/p' input
This is a CERTIFICATE Request file:
CERTIFICATE Subject:

<13>把文本中全部的seu替换为njue,如果只想匹配行第一个出现的seu不加g即可sed -n 's/seu/njue/p' input ,只想匹配全文第三次出现的怎么办sed -n 's/seu/njue/3p' input

[root@git ~]# sed -n 's/seu/njue/pg' input
It should be mailed to zawu@njue.edu.cn
/O=Grid/OU=GlobusTest/OU=simpleCA-njuegridl.njue.edu.cn/OU=njue.edu.cn/CN=globus

<14>将seu改为njue的结果写入output文件

[root@git ~]# sed -n 's/seu/njue/w output' input
[root@git ~]# cat output
It should be mailed to zawu@njue.edu.cn
/O=Grid/OU=GlobusTest/OU=simpleCA-njuegridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

<15>在匹配“file:”行后读取output文件的内容

[root@git ~]# sed '/file:/r output' input
This is a Certificate Request file:
It should be mailed to zawu@njue.edu.cn
/O=Grid/OU=GlobusTest/OU=simpleCA-njuegridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

It should be mailed to zawu@seu.edu.cn

====================================================
Certificate Subject:

/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.

/home/alloy/linuxshell/CH02/usercert.pem

<16>将文件中f、m、j字符变为大写字母

[root@git ~]# sed 'y/ymj/YMJ/' input
This is a Certificate Request file:

It should be Mailed to zawu@seu.edu.cn

====================================================
Certificate SubJect:

/O=Grid/OU=GlobusTest/OU=siMpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

The above string is known as Your user certificate subJect,and it uniquelY identifies this user.$88
To install this user certificate,please save this e-Mail Message into the following file.

/hoMe/alloY/linuxshell/CH02/usercert.peM

<17>将文件中与Certificate匹配的行将全部的i替换为I,将第一个le替换为99

[root@git ~]# sed '/Certificate/{s/i/I/g;s/le/99/;}' input
ThIs Is a CertIfIcate Request fI99:

It should be mailed to zawu@seu.edu.cn

====================================================
CertIfIcate Subject:

/O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus

The above string is known as your user certificate subject,and it uniquely identifies this user.$88
To install this user certificate,please save this e-mail message into the following file.

/home/alloy/linuxshell/CH02/usercert.pem

sed命令讲解的更多相关文章

  1. sed命令详解-应用篇

    本篇从实用的角度讲解sed,关于sed的详细帮助文档,请参考前篇 http://www.cnblogs.com/the-capricornus/p/5279979.html 本篇用到的选项请参考前篇. ...

  2. sed命令 linux

    sed 实用工具是一个"编辑器",但它与其它大多数编辑器不同.除了不面向屏幕之外,它还是非交互式的.这意味着您必须将要对数据执行的命令插入到命令行或要处 理的脚本中.当显示它时,请 ...

  3. sed命令使用详解

        内容来自马哥视频,感谢马哥精彩讲解 sed:编辑器 sed: Stream EDitor, 行编辑器,逐行进行处理 grep:实现文本过滤 awk:文本报告生成器 sed默认不编辑源文件,而是 ...

  4. 文本处理三剑客之sed命令

    第十八章.文本处理三剑客之sed命令 目录 sed介绍 sed命令常用选项 sed常用编辑命令 sed使用示例 sed高级语法 18.1.sed简介 sed全名stream editor,流编辑器,s ...

  5. linux shell 用sed命令在文本的行尾或行首添加字符

    转自 http://www.cnblogs.com/aaronwxb/archive/2011/08/19/2145364.html 昨天写一个脚本花了一天的2/3的时间,而且大部分时间都耗在了sed ...

  6. linux sed命令详解

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...

  7. sed命令详解

    搜索 纠正错误  添加实例 sed 功能强大的流式文本编辑器 补充说明 sed 是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时 ...

  8. Linux安全基础:sed命令的使用

    sed 是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作. Sed本质上是一个编辑器,但是它是非交互式的,这点与VIM不同:同时 ...

  9. [转]sed命令详解

    转载:http://blog.chinaunix.net/u/22677/showart_1076318.html   1.简介 sed是非交互式的编辑器.它不会修改文件,除非使用shell重定向来保 ...

随机推荐

  1. spring security 学习

    1.默认登录   user  /df1fc617-bb94-494e-8adb-0234046bf092 取消校验 在启动类上添加下面的注解 @EnableAutoConfiguration(excl ...

  2. DateTime类使用二:常用时间段操作(经典)

    //大家在做报表或查询的时候都会有给用户预设一些可选的日期范围(如上图)                //如本年度销售额.本季度利润.本月新增客户                //C#里内置的Da ...

  3. 视觉显著性简介 Saliency Detection

    内容转移到博客文章系列:显著性检测 1.简介 视觉显著性包括从下而上和从上往下两种机制.从下而上也可以认为是数据驱动,即图像本身对人的吸引,从上而下则是在人意识控制下对图像进行注意.科研主要做的是从下 ...

  4. win10安装java

    java安装还比较顺利,贴两篇亲测可行的教程 1.开发环境安装:https://www.cnblogs.com/shirley-0021/p/8510051.html 2.开发工具安装(Eclipse ...

  5. CSS中的颜色问题

    css颜色: CSS 颜色 颜色是通过对红.绿和蓝光的组合来显示的 颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义.对光源进行设置的最低值可以是 0(十 ...

  6. Shell 有类型变量

    1.通过 declare 和 typeset 命令 declare 和 typeset 两者等价 declare 和 typeset 都是用来定义变量类型的 下面以 declare 进行总结 2.de ...

  7. less &进行选择判断css的样式

    先说&在less写 .parent{ .child{} &.and{} }在css就是 .parent.child{}//父子关系 .parent.and{}//并关系 用到这个方法是 ...

  8. dos5章

    一.用set命令设置自定义变量 显示.设置或删除 cmd.exe 环境变量. SET [variable=[string]]variable 指定环境变量名.string 指定要指派给变量的一系列字符 ...

  9. 网络-Cisco

    选择题(每题2分,共20分) 1.192.168.11.1/23的网络地址是(  A) A.192.168.10.0 B.192.168.11.0 C.192.168.10.255 D.192.168 ...

  10. H5介绍与测试设计

    近期的项目中接触的基本都为H5的测试工作,从项目初期评审到测试工作的完成过程中,遇到了很多问题是与APP测试方法不太相同的地方,在此希望总结测试过程遇到的问题及新思路给之后会接触到H5测试的同学. 这 ...