Shell之Sed命令-yellowcong 系统管理员 2022-06-06 07:25 162阅读 0赞 > Sed可以用来替换文本,`sed -i '/xx/xxx/p' file`来替换文件类容,-i表示更改文件,如果不加上参数-i,只是替换了,但是没有写入到文件里面。还有,路径的替换是比较特殊的,需要特别的注意 ## 替换命令 ## ## 替换路径 ## Mac上使用sed命令时,报出sed: 1: “1.txt”: invalid command code .错误。是由于Mac上sed命令与linux下稍有不同。Mac上默认提供修改时的备份机制。 sed -i "" "s#E:\\\\开发\\\\yellow\\\\images\\\\#../images/#g" ./README.md #需要备份的情况 sed -i ".bak" 's/string_old/string_new/g' grep -rl 'string_old' ./ ### 全面替换标记g ### 使用后缀 /g 标记会替换每一行中的所有匹配: 当需要从第N处匹配开始替换时,可以使用 /Ng: #替换所有的name echo name_xx_name_xx2 | sed 's/name/NAME/g' #替换第二次的name为大写 echo name_xx_name_xx2 | sed 's/name/NAME/2g' 查看结果,可以看到,这个 g可以替换当前行的第几个 ![这里写图片描述][aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYwNDQyMzY1] ### 路径替换 ### zoo\_sample.cfg配置文件,实验替换文件 # The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/tmp/zookeeper # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 路径的替换,需要结合\#好表示替换的类容,而不是分号来替换 #查找/tmp/zookeeper 的文件 sed -n '/\/tmp\/zookeeper/p' test.cfg #替换路径的操作 sed -i 's#/tmp/zookeeper#/tmp/zookeeper1/data#' test.cfg ![这里写图片描述][aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYzMzE3NjA0] ### 追加行 ### #在/dataDir/ 后面添加一行 sed -i '/^dataDir/a\dataLogDir=\/tmp\/zookeeper1\/logs' test.cfg #在dataDir 前面添加一样 sed -i '/^dataDir/i\dataLogDir=\/tmp\/zookeeper1\/logs' test.cfg 在后面添加数据,参数是 a(after), ![这里写图片描述][aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYzNzQ4MjUz] 在前面添加数据,参数是i(insert) ![这里写图片描述][aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYzOTMwMDg5] ### 文件头/尾追加 ### #追加的数据太长了,所以使用了 \ 进行了换行的操作 sed -i '$a \server.1=127.0.0.1:2222:2225 \ server.2=127.0.0.1:3333:3335 \ server.3=127.0.0.1:4444:4445 \ ' test.cfg #在文件的第一行添加文件 sed -i '1 i\fisrt file' test.cfg 添加成功 ![这里写图片描述][aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTcwNzM3ODE0] ## 最后一行添加 ## ## 删除命令 ## 删除的操作,可以删除哪一行,也可以设定匹配删除哪一行的数据 ### 行删除 ### #删除第一行 sed -i '1d' test.cfg #删除第二行,第三行,第四行有此类推 sed -i '2d' test.cfg #删除最后一行 sed -i '$d' test.cfg ![这里写图片描述][aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTY1NTQxNTAw] ### 匹配删除 ### sed -i '//d' test.cfg ![这里写图片描述][aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTY1ODEwMzUx] ## 查找命令 ## ### 查找文件行数据 ### #查找文件的第二行 sed -n '2p' zoo.cfg #打印文件的1-6 行的数据 sed -n '1,6p' zoo.cfg #打印以data开头的行 sed -n '/^data/p' zoo.cfg ![这里写图片描述][aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYxNzA0Nzk2] ## 参考文章 ## [http://www.cnblogs.com/ctaixw/p/5860221.html][http_www.cnblogs.com_ctaixw_p_5860221.html] [http://man.linuxde.net/sed\#sed替换标记][http_man.linuxde.net_sed_sed] [http://blog.csdn.net/loveaborn/article/details/17269645][http_blog.csdn.net_loveaborn_article_details_17269645] [aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYwNDQyMzY1]: /images/20220606/015789fdd6d94f03996be72982da7dfb.png [aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYzMzE3NjA0]: /images/20220606/2ecee0f3b85143829dd38a8cbc944f23.png [aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYzNzQ4MjUz]: /images/20220606/e7f85f329dfd4690b945371c5eced8c5.png [aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYzOTMwMDg5]: /images/20220606/c0968099cca6480f82aebff07ad6fcf1.png [aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTcwNzM3ODE0]: /images/20220606/56327938276142c79cfdbe4dd0ef6e21.png [aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTY1NTQxNTAw]: /images/20220606/c5677f101292403b92f5372d2c467b45.png [aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTY1ODEwMzUx]: /images/20220606/e1ade727dc7e489387bc600bfe99cf9b.png [aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMTAyMTYxNzA0Nzk2]: /images/20220606/93b2d5701d2a4848bc49a8ebe6f83b93.png [http_www.cnblogs.com_ctaixw_p_5860221.html]: http://www.cnblogs.com/ctaixw/p/5860221.html [http_man.linuxde.net_sed_sed]: http://man.linuxde.net/sed#sed%E6%9B%BF%E6%8D%A2%E6%A0%87%E8%AE%B0 [http_blog.csdn.net_loveaborn_article_details_17269645]: http://blog.csdn.net/loveaborn/article/details/17269645
还没有评论,来说两句吧...