sed命令讲解

时间:2021-03-20 15:46:37

 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