shell 常用命令之四 sed
来源:互联网 发布:新唐单片机官网 编辑:程序博客网 时间:2024/06/08 13:45
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 :变换字符
四、举例
[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/=' input16[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 常用命令之四 sed
- shell四剑客之sed
- shell命令-sed常用命令
- Linux常用命令之sed
- Shell---Shell常用命令(tr、sed、cut)
- shell之sed
- Shell之Sed
- shell编程之sed
- linux shell之sed
- shell学习之sed
- shell 之 sed命令
- Shell之Sed
- linux shell之sed
- linux shell之sed
- shell之sed
- shell编程之sed
- linux shell之sed
- linux shell之sed
- 通过存储过程解决并发生成唯一订单号
- 工作记录---转角遇到的EXCEL技巧(1)
- POJ-2909
- poj 1463 Strategic game 树的最小点覆盖
- C指针声明解读之左右法则
- shell 常用命令之四 sed
- Ubuntu 下 Apache2服务器的搭建
- RedHatLinux安装桌面
- UIButton 使用注意事项
- 构造方法
- linux下syslog使用说明
- matlab BP神经网络
- Permission denied: user=root, access=EXECUTE, inode="/tmp/hadoop-yarn":grid:supergroup:drwx------
- TextView的常用属性