1. sed 普通替换类
DEVICE=”eth0″
HWADDR=”00:11:85:FE:F6:0B”
NM_CONTROLLED=”yes”
ONBOOT=”yes”
TYPE=”Ethernet”
IPADDR=114.113.221.49
NETMASK=255.255.255.0
DNS1=202.106.46.151
DNS2=202.106.0.20
上面表示一个示例文件ifcfg-eth0
我们修改NETMASK=255.255.255.0 为 NETMASK=255.255.0.0
sed ’s/NETMASK=255.255.255.0/NETMASK=255.255.0.0/’ ifcfg-eth0
2. sed 直接修改文件
我们可以通过 sed -i ’s/NETMASK=255.255.255.0/NETMASK=255.0.0.0/g’ ifcfg-eth0 来直接编辑文件,用于非交互式环境。
又比如要注释掉IPADDR=114.113.221.49 这行:
sed -i ’s/IPADDR=114.113.221.49/#IPADDR=114.113.221.49/g’ ifcfg-eth0
3. sed 的特殊替换
当sed要替换的内容中含有“/”等特殊符号时候咱们可以通过加“\”使其转意,也可以像vim一样使用#使其后面的全部转意,如下
sed -i ’s#/home/inits/#/usr/home/#’ ifcfg-eth0
4. sed 的删除使用
当我们要用sed修改文本时候通常会用到删除这个命令,我们可以在sed中进行。
sed -i ’/TYPE=”google”/d’ ifcfg-eth0
sed 基础类使用方法
1. sed命令的-n 选项
sed -n 的用法,当sed带n时候,只打印特定行,如果不带n则会打印出所有的行
[inits@startup ~]$ sed -n ’1p’ input
This is a Certificate Request file:
[inits@startup ~]$ sed ’1p’ input
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-seugrid1.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/globus/.globus/usercert.pem
2. sed 命令打印范围行
[inits@startup ~]$ sed -n ’3,6p’ input
It should be mailed to zawu@seu.edu.cn
3. sed 命令打印匹配模式行(sed对大小写是敏感的)
[inits@startup ~]$ 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. sed 命令的-e 选项
-e 选项表示将下一个字符串解析为sed 编辑命令,如果只传递一个编辑命令给sed,-e 选项就可以不用,如下例,
命令利用sed编辑命令”=” 号打印匹配Certificate 关键字的行号,结果为1和6,如果需要将与匹配Certificate 关键字行
的内容和行号都打印出来,就要向sed 传递”p” 和”=” 两个编辑命令,此时我们需要使用-e 选项,-e选项指定其后面紧跟着的字符串为sed编辑命令
,打印匹配行内容及其行号的命令如下:
[inits@startup ~]$ sed -n ’/Certificate/=’ input “=” 表示打印行号
1
8
[inits@startup ~]$ sed -n -e ’/Certificate/p’ -e ’/Certificate/=’ input
This is a Certificate Request file:
1
Certificate Subject:
8
需要注意的是,sed命令不支持同时带有多个编辑命令的用法,如:
[inits@startup ~]$ sed -n ’Certificate/p=’ input
sed: -e expression #1, char 1: unknown command: `C’
5. sed 命令的-f 选项
-f 选项只有调用sed脚本文件时才起作用。
6. sed 匹配元字符
如果sed命令所要匹配的目标字符串中包含元字符,需要使用转义字符”\” 屏蔽其特殊意义
[inits@startup ~]$ sed -n ’/\./p’ input
It should be mailed to zawu@seu.edu.cn
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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/globus/.globus/usercert.pem
7. sed 命令使用元字符进行匹配
sed 的基本编辑命令可以放在单引号内,也可以放在单引号外
[inits@startup ~]$ sed -n ’$p’ input
/home/globus/.globus/usercert.pem
[inits@startup ~]$ sed -n ’$'p input
/home/globus/.globus/usercert.pem
元字符匹配任意字符串
[inits@startup ~]$ sed -n ’/.*bus/p’ input
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.seu.edu.cn/OU=seu.edu.cn/CN=globus
/home/globus/.globus/usercert.pem
8. sed 命令的!号
!号表示取反,x,y!表示匹配不再x和y行号范围内的行,下例利用sed命令打印不再2~10 之间的行
同时!符号,不能用于匹配关键字,如无法表示不与/pattern/匹配的行
[inits@startup ~]$ sed -n ’2,10!p’ input
This is a Certificate Request file:
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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/globus/.globus/usercert.pem
9. 使用行号与关键字匹配限定行范围
打印与seugrid 的匹配行到最后一行
[inits@startup ~]$ sed -n ’/seugrid/,$p’ input
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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/globus/.globus/usercert.pem
第2条命令;打印第3行到与Certificate 的匹配行
[inits@startup ~]$ sed -n ’3,/seugrid/p’ input
It should be mailed to zawu@seu.edu.cn
==============================================================
Certificate Subject:
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.seu.edu.cn/OU=seu.edu.cn/CN=globus
10. sed 命令插入文本
插入文本和追加文本类似,区别仅在于追加文本是在匹配行的后面插入,而插入文本是在匹配行的前面插入,sed编辑命令的插入文本符号为i\
插入文本的格式为:
sed ’/指定地址/i\text’ 输入文件
[inits@startup ~]$ 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:
11. sed 命令删除文本
sed 删除文本命令可以将指定行或指定行范围进行删除,sed 命令的删除文本号为d,删除文本的格式为 sed ’/指定地址/d’
[inits@startup ~]$ sed ’1d’ input
It should be mailed to zawu@seu.edu.cn
==============================================================
Certificate Subject:
[inits@startup ~]$ sed ’$d’ input
This is a Certificate Request file:
It should be mailed to zawu@seu.edu.cn
==============================================================
Certificate Subject:
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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.
[inits@startup ~]$ sed ’1,10d’ input
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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/globus/.globus/usercert.pem
12. sed 命令替换文本
[inits@startup ~]$ sed ’s/Certificate/CERTIFICATE/’ input
This is a CERTIFICATE Request file:
It should be mailed to zawu@seu.edu.cn
==============================================================
使用 -n 和-p 选项结合使用,只打印替换行
[inits@startup ~]$ sed -n ’s/Certificate/CERTIFICATE/p’ input
This is a CERTIFICATE Request file:
CERTIFICATE Subject:
13 . sed 命令写入一个新文件
[inits@startup ~]$ sed -n ’1,5 w output’ input
15. sed 命令执行命令组
sed 编辑命令中的”{}” 符号可以指定在定位行上所执行的命令组,它的作用与sed的-e选项类似,都是为了在定位执行多个编辑命令
下例说明”{}” 执行命令组的使用方法:
[inits@startup ~]$ sed -n -e ’/Certificate/p’ -e ’/Certificate/=’ input
This is a Certificate Request file:
1
Certificate Subject:
8
[inits@startup ~]$ sed -n ’/Certificate/{p;=}’ input
This is a Certificate Request file:
1
Certificate Subject:
8
[inits@startup ~]$
16. sed 命令利用分号分割多个编辑命令
除了使用-e 和{} 符号可以实现sed 的多编辑命令之外,利用分号(:) 也可以实现类似功能
[inits@startup ~]$ sed ’s/globus/GLOBUS/; s/seugrid/SEUGRID/’ input
This is a Certificate Request file:
It should be mailed to zawu@seu.edu.cn
==============================================================
Certificate Subject:
/O=Grid/OU=GlobusTest/OU=simpleCA-SEUGRID1.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/GLOBUS/.globus/usercert.pem
[inits@startup ~]$
DEVICE=”eth0″
HWADDR=”00:11:85:FE:F6:0B”
NM_CONTROLLED=”yes”
ONBOOT=”yes”
TYPE=”Ethernet”
IPADDR=114.113.221.49
NETMASK=255.255.255.0
DNS1=202.106.46.151
DNS2=202.106.0.20
上面表示一个示例文件ifcfg-eth0
我们修改NETMASK=255.255.255.0 为 NETMASK=255.255.0.0
sed ’s/NETMASK=255.255.255.0/NETMASK=255.255.0.0/’ ifcfg-eth0
2. sed 直接修改文件
我们可以通过 sed -i ’s/NETMASK=255.255.255.0/NETMASK=255.0.0.0/g’ ifcfg-eth0 来直接编辑文件,用于非交互式环境。
又比如要注释掉IPADDR=114.113.221.49 这行:
sed -i ’s/IPADDR=114.113.221.49/#IPADDR=114.113.221.49/g’ ifcfg-eth0
3. sed 的特殊替换
当sed要替换的内容中含有“/”等特殊符号时候咱们可以通过加“\”使其转意,也可以像vim一样使用#使其后面的全部转意,如下
sed -i ’s#/home/inits/#/usr/home/#’ ifcfg-eth0
4. sed 的删除使用
当我们要用sed修改文本时候通常会用到删除这个命令,我们可以在sed中进行。
sed -i ’/TYPE=”google”/d’ ifcfg-eth0
sed 基础类使用方法
1. sed命令的-n 选项
sed -n 的用法,当sed带n时候,只打印特定行,如果不带n则会打印出所有的行
[inits@startup ~]$ sed -n ’1p’ input
This is a Certificate Request file:
[inits@startup ~]$ sed ’1p’ input
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-seugrid1.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/globus/.globus/usercert.pem
2. sed 命令打印范围行
[inits@startup ~]$ sed -n ’3,6p’ input
It should be mailed to zawu@seu.edu.cn
3. sed 命令打印匹配模式行(sed对大小写是敏感的)
[inits@startup ~]$ 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. sed 命令的-e 选项
-e 选项表示将下一个字符串解析为sed 编辑命令,如果只传递一个编辑命令给sed,-e 选项就可以不用,如下例,
命令利用sed编辑命令”=” 号打印匹配Certificate 关键字的行号,结果为1和6,如果需要将与匹配Certificate 关键字行
的内容和行号都打印出来,就要向sed 传递”p” 和”=” 两个编辑命令,此时我们需要使用-e 选项,-e选项指定其后面紧跟着的字符串为sed编辑命令
,打印匹配行内容及其行号的命令如下:
[inits@startup ~]$ sed -n ’/Certificate/=’ input “=” 表示打印行号
1
8
[inits@startup ~]$ sed -n -e ’/Certificate/p’ -e ’/Certificate/=’ input
This is a Certificate Request file:
1
Certificate Subject:
8
需要注意的是,sed命令不支持同时带有多个编辑命令的用法,如:
[inits@startup ~]$ sed -n ’Certificate/p=’ input
sed: -e expression #1, char 1: unknown command: `C’
5. sed 命令的-f 选项
-f 选项只有调用sed脚本文件时才起作用。
6. sed 匹配元字符
如果sed命令所要匹配的目标字符串中包含元字符,需要使用转义字符”\” 屏蔽其特殊意义
[inits@startup ~]$ sed -n ’/\./p’ input
It should be mailed to zawu@seu.edu.cn
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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/globus/.globus/usercert.pem
7. sed 命令使用元字符进行匹配
sed 的基本编辑命令可以放在单引号内,也可以放在单引号外
[inits@startup ~]$ sed -n ’$p’ input
/home/globus/.globus/usercert.pem
[inits@startup ~]$ sed -n ’$'p input
/home/globus/.globus/usercert.pem
元字符匹配任意字符串
[inits@startup ~]$ sed -n ’/.*bus/p’ input
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.seu.edu.cn/OU=seu.edu.cn/CN=globus
/home/globus/.globus/usercert.pem
8. sed 命令的!号
!号表示取反,x,y!表示匹配不再x和y行号范围内的行,下例利用sed命令打印不再2~10 之间的行
同时!符号,不能用于匹配关键字,如无法表示不与/pattern/匹配的行
[inits@startup ~]$ sed -n ’2,10!p’ input
This is a Certificate Request file:
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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/globus/.globus/usercert.pem
9. 使用行号与关键字匹配限定行范围
打印与seugrid 的匹配行到最后一行
[inits@startup ~]$ sed -n ’/seugrid/,$p’ input
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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/globus/.globus/usercert.pem
第2条命令;打印第3行到与Certificate 的匹配行
[inits@startup ~]$ sed -n ’3,/seugrid/p’ input
It should be mailed to zawu@seu.edu.cn
==============================================================
Certificate Subject:
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.seu.edu.cn/OU=seu.edu.cn/CN=globus
10. sed 命令插入文本
插入文本和追加文本类似,区别仅在于追加文本是在匹配行的后面插入,而插入文本是在匹配行的前面插入,sed编辑命令的插入文本符号为i\
插入文本的格式为:
sed ’/指定地址/i\text’ 输入文件
[inits@startup ~]$ 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:
11. sed 命令删除文本
sed 删除文本命令可以将指定行或指定行范围进行删除,sed 命令的删除文本号为d,删除文本的格式为 sed ’/指定地址/d’
[inits@startup ~]$ sed ’1d’ input
It should be mailed to zawu@seu.edu.cn
==============================================================
Certificate Subject:
[inits@startup ~]$ sed ’$d’ input
This is a Certificate Request file:
It should be mailed to zawu@seu.edu.cn
==============================================================
Certificate Subject:
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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.
[inits@startup ~]$ sed ’1,10d’ input
/O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.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/globus/.globus/usercert.pem
12. sed 命令替换文本
[inits@startup ~]$ sed ’s/Certificate/CERTIFICATE/’ input
This is a CERTIFICATE Request file:
It should be mailed to zawu@seu.edu.cn
==============================================================
使用 -n 和-p 选项结合使用,只打印替换行
[inits@startup ~]$ sed -n ’s/Certificate/CERTIFICATE/p’ input
This is a CERTIFICATE Request file:
CERTIFICATE Subject:
13 . sed 命令写入一个新文件
[inits@startup ~]$ sed -n ’1,5 w output’ input
15. sed 命令执行命令组
sed 编辑命令中的”{}” 符号可以指定在定位行上所执行的命令组,它的作用与sed的-e选项类似,都是为了在定位执行多个编辑命令
下例说明”{}” 执行命令组的使用方法:
[inits@startup ~]$ sed -n -e ’/Certificate/p’ -e ’/Certificate/=’ input
This is a Certificate Request file:
1
Certificate Subject:
8
[inits@startup ~]$ sed -n ’/Certificate/{p;=}’ input
This is a Certificate Request file:
1
Certificate Subject:
8
[inits@startup ~]$
16. sed 命令利用分号分割多个编辑命令
除了使用-e 和{} 符号可以实现sed 的多编辑命令之外,利用分号(:) 也可以实现类似功能
[inits@startup ~]$ sed ’s/globus/GLOBUS/; s/seugrid/SEUGRID/’ input
This is a Certificate Request file:
It should be mailed to zawu@seu.edu.cn
==============================================================
Certificate Subject:
/O=Grid/OU=GlobusTest/OU=simpleCA-SEUGRID1.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/GLOBUS/.globus/usercert.pem
[inits@startup ~]$