首页 > 其他 > 详细

sed 替换命令使用

时间:2021-09-04 04:33:20      阅读:33      评论:0      收藏:0      [点我收藏+]

输入文件不会被修改,sed 只在模式空间中执行替换命令,然后输出模式空间的
内容。
文本文件 employee.txt

101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager


1.用 Director 替换所有行中的 Manager

替换命令用到参数s 注意代码格式

[root@localhost /]# sed s/Manager/Director/ employee.txt
101,John Doe,CEO
102,Jason Smith,IT Director
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Director

 


2.只把包含 Sales 的行中的 Manager 替换为 Director

[root@localhost /]# sed /Sales/s/Manager/Director/ employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Director

 

3.全局标志 g

用大写 A 替换第一次出现的小写字母 a

[root@localhost /]# sed s/a/A/ employee.txt
101,John Doe,CEO
102,JAson Smith,IT Manager
103,RAj Reddy,Sysadmin
104,AnAnd Ram,Developer
105,JAne Miller,Sales Manager

 

把所有小写字母 a 替换为大写字母 A

[root@localhost /]# sed s/a/A/g employee.txt
101,John Doe,CEO
102,JAson Smith,IT MAnAger
103,RAj Reddy,SysAdmin
104,AnAnd RAm,Developer
105,JAne Miller,SAles MAnAger

 

4.数字标志

把第二次出现的小写字母 a 替换为大写字母 A

[root@localhost /]# sed s/a/A/2 employee.txt
101,John Doe,CEO
102,Jason Smith,IT MAnager
103,Raj Reddy,SysAdmin
104,Anand RAm,Developer
105,Jane Miller,SAles Manager


建立一下文件

vi substitute-locate.txt
locate command is used to locate files
locate command uses database to locate files
locate command can also use regex for searching

 

用刚才建立的文件,把每行中第二次出现的 locate 替换为 find

[root@localhost /]# sed s/locate/find/2 substitute-locate.txt
locate command is used to find files
locate command uses database to find files
locate command can also use regex for searching

 

5.打印标志 p


只打印替换后的行

[root@localhost /]# sed -n s/John/johnny/p employee.txt
101,johnny Doe,CEO

 


把每行中第二次出现的 locate 替换为 find 并打印出来

[root@localhost /]# sed -n s/locate/find/2p substitute-locate.txt
locate command is used to find files
locate command uses database to find files

 

6.写标志w

标志 w 代表 write,当替换操作执行成功后,它把替换后的结果保存的文件。
把每行出现的 John 替换为 Johnny,只把替换后的内容写到 output.txt 中

[root@localhost /]# sed -n s/John/Johnny/w output.txt employee.txt
[root@localhost /]# cat output.txt
101,Johnny Doe,CEO

 

把每行第二次出现的 locate 替换为 find,把替换的结果保存到文件中,同时显示输入文件所有内容

[root@localhost /]# sed s/locate/find/2w output.txt substitute-locate.txt
locate command is used to find files
locate command uses database to find files
locate command can also use regex for searching
[root@localhost /]# cat output.txt
locate command is used to find files
locate command uses database to find files

 

7.忽略大小写标志 i


把 john 或 John 替换为 Johnny,但是文件中的John中的J是大写,所以用i参数忽略大小写

[root@localhost /]# sed s/john/Johnny/i employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

 


8.执行命令标志 e

如下图所示,e参数就是执行的作用 文本文件ls -l /etc/passwd e参数则是执行这条命令

[root@localhost /]# vi /files.txt
[root@localhost /]# cat /files.txt
/etc/passwd
/etc/group
[root@localhost /]# sed s/^/ls -l/ files.txt
ls -l/etc/passwd
ls -l/etc/group
[root@localhost /]# sed s/^/ls -l /e files.txt
-rw-r--r--. 1 root root 1766 10月 24 15:15 /etc/passwd
-rw-r--r--. 1 root root 920 10月 24 15:15 /etc/group

 



原文链接:https://blog.csdn.net/weixin_43150761/article/details/83152456

sed 替换命令使用

原文:https://www.cnblogs.com/fengfengyang/p/15221361.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!