测试文本内容:
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 into the following file.
/home/globus/.globus/usercert.pem
-n的使用
定位
匹配元字符
如果sed命令匹配的目标字符串中包含元字符,需要使用转义符""屏蔽其特殊意义
sed -n '/./p' input
使用元字符进行匹配($符)
$在正则中表示行尾,但是在sed命令中却表示最后一行
sed -n '$p' input
结果:
/home/globus/.globus/usercert.pem
编辑
插入文本 /i
直接引用行号
sed '1i hello' input #在第一行之前插入一行,内容为hello
模式匹配之后
sed '/file:/i hello' input #所有匹配file:的行之前,添加一行内容为hello
*(行)后追加使用a,原理相同,下面举两例,后省略:
sed '1a hello' input
sed '/file:/a we add this line' input
修改文本 /c
使用c标记
sed '/file:/c we edit this line' input #此行相当于被覆盖重写
删除文本
单行:sed '1d' input
多行:sed '1,3d' input
替换文本
s/被替换的文本/替换文本/替换选项
替换之后再打印修改的行
补充(与模式、或模式):
同时匹配ABC 和 123:
sed -n '/ABC/{/123/p}' awk '/ABC/&&/123/{ print $0 }' grep -E '(ABC.*123|123.*ABC)'
匹配ABC 或 123:
sed -n '/(ABC|123)/p' awk '/ABC/||/123/{ print $0 }' grep -E '(ABC|123)' 或 egrep 'ABC|123'
样式:
/(A|B|C|D|...)/p