shell 常用命令之四 sed
2015-12-11 10:03
441 查看
sed 可以 对文本文件和标准输入进行编辑(键盘输入、文件重定向、字符串、变量、管道文本)。
sed [选项] ‘sed 命令’ 输入文件 //sed对输入文件进行处理
| sed [选项] ‘sed 命令’ //加一个管道,说明sed处理的是从管道中读取的数据,就没必要加 输入文件
当然也可以 cat /etc/passwd | sed -n '/root/p' //显示以root关键字 开头的行
值得注意的是:sed只是对缓冲区中原始文件的的副本进行编辑,并不编辑原始的文件
命令格式:
一、选项
-n:不打印所有的行到标准输出
-e:表示键下一个字符串解释为sed 编辑命令
-f:表示正在调用sed文本
-i :表示在原文本中修改
二、sed命令定位文本的方法
x :x为指定行号
x,y :指定从x到y的行号范围
x,y! :查询不包括从x到y的行号范围
/pattern/ :查询包含模式的行
/pattern/pattern/ :查询包含两个模式的行 //注意:模式间的分隔符不是写死的,可以随意改动,只要三个统一就可以
/pattern/,x :从与模式匹配的行到x行号之间的行
x,/pattern/ :从第x行到与pattern相匹配的行之间的行
三、sed编辑命令
p :打印匹配的行
= :打印文件的行号
a\ :在定位行号之后追加文本信息
i\ :在定位行号之前追加文本信息
d :删除定位行
c\ :用新的文本定位文本
s :使用替换模式替换相应的模式
r :从另一个文本中读文本
w :将文本写入到一个文件
y :变换字符
四、举例
在这在简单说一下-i的用法:sed -i 指定行号 /s被替换模式/替换成模式/ 文件;当然模式间的分隔符可以随便修改,只要三个一样就行。
因此,还有一个表达形式:sed -i "行号"“s@”“str1”“@str2@” 文件 //注意: 黄色部分之间没有空格
例子不在一一在写,自己写个小程序练一下就Ok。
sed [选项] ‘sed 命令’ 输入文件 //sed对输入文件进行处理
| sed [选项] ‘sed 命令’ //加一个管道,说明sed处理的是从管道中读取的数据,就没必要加 输入文件
当然也可以 cat /etc/passwd | sed -n '/root/p' //显示以root关键字 开头的行
值得注意的是:sed只是对缓冲区中原始文件的的副本进行编辑,并不编辑原始的文件
命令格式:
一、选项
-n:不打印所有的行到标准输出
-e:表示键下一个字符串解释为sed 编辑命令
-f:表示正在调用sed文本
-i :表示在原文本中修改
二、sed命令定位文本的方法
x :x为指定行号
x,y :指定从x到y的行号范围
x,y! :查询不包括从x到y的行号范围
/pattern/ :查询包含模式的行
/pattern/pattern/ :查询包含两个模式的行 //注意:模式间的分隔符不是写死的,可以随意改动,只要三个统一就可以
/pattern/,x :从与模式匹配的行到x行号之间的行
x,/pattern/ :从第x行到与pattern相匹配的行之间的行
三、sed编辑命令
p :打印匹配的行
= :打印文件的行号
a\ :在定位行号之后追加文本信息
i\ :在定位行号之前追加文本信息
d :删除定位行
c\ :用新的文本定位文本
s :使用替换模式替换相应的模式
r :从另一个文本中读文本
w :将文本写入到一个文件
y :变换字符
四、举例
[root@zhangna ~]# cat input This is a Certificate Request file: It should be mailed to na.zhang@i-soft.com.cn ================================================================================== Certificate Subject: /O=Grid/OU=GLOBUSTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus The above string is konwn 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 [root@zhangna ~]# sed '1p' input //没有选项-n,结果:将第一行打印出来之后,会将整个文本打印出来 This is a Certificate Request file: This is a Certificate Request file: It should be mailed to na.zhang@i-soft.com.cn ================================================================================== Certificate Subject: /O=Grid/OU=GLOBUSTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus The above string is konwn 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 [root@zhangna ~]# sed '3p' input This is a Certificate Request file: It should be mailed to na.zhang@i-soft.com.cn It should be mailed to na.zhang@i-soft.com.cn ================================================================================== Certificate Subject: /O=Grid/OU=GLOBUSTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus The above string is konwn 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 [root@zhangna ~]# sed -n '3p' input //加上选项-n,只打印选中的行 It should be mailed to na.zhang@i-soft.com.cn [root@zhangna ~]# sed -n '3,6p' input //打印3~6行 It should be mailed to na.zhang@i-soft.com.cn ================================================================================== Certificate Subject: [root@zhangna ~]# sed -n '/certificate/p' input //打印与/certificate/匹配的行 The above string is konwn 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. [root@zhangna ~]# sed -n '/Certificate/=' input 1 6 [root@zhangna ~]# sed -n -e '/Certificate/=' -e '/Certificate/p' input //只有向sed命令传递多个编辑命令时用-e才有意义 ;;;;这是前两个命令汇在了一起 1 This is a Certificate Request file: 6 Certificate Subject: [root@zhangna ~]# [root@zhangna ~]# sed '/file:/a\this is a new add.' input // a\表示追加到匹配行后 This is a Certificate Request file: this is a new add. It should be mailed to na.zhang@i-soft.com.cn ================================================================================== Certificate Subject: /O=Grid/OU=GLOBUSTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus The above string is konwn 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 下面看一下参数-f 怎么使用 <pre name="code" class="objc">[root@zhangna ~]# cat append.sed #!/bin/sed -f /file:/a\ We append a new line.\ So we are very happy. [root@zhangna ~]# ./append.sed input This is a Certificate Request file: We append a new line. So we are very happy. It should be mailed to na.zhang@i-soft.com.cn ================================================================================== Certificate Subject: /O=Grid/OU=GLOBUSTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus The above string is konwn 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
在这在简单说一下-i的用法:sed -i 指定行号 /s被替换模式/替换成模式/ 文件;当然模式间的分隔符可以随便修改,只要三个一样就行。
因此,还有一个表达形式:sed -i "行号"“s@”“str1”“@str2@” 文件 //注意: 黄色部分之间没有空格
例子不在一一在写,自己写个小程序练一下就Ok。
相关文章推荐
- shell中如何delay
- 一个简单的shell查找脚本
- shell 如何判断命令行参数个数
- LInux 开发板 shell 中退格显示^H
- [转] 用管道获得shell 命令的输出
- sql server 开xp_cmdhshell
- shell学习笔记
- BeanShell语法学习提要
- grep指定文件中的所有内容
- Shell学习--(3)替换,字符串,数组
- powershell脚本-DNS
- shell变量数学运算
- Shell学习--(2)变量
- Shell学习--(1)
- Bash环境配置文件
- shell学习笔记
- shell中“>/dev/null 2>&1”详解
- 关于bash中的数据流重导向
- 关于bash中的数据流重导向
- nodejs 执行shell 命令