shell 批量替换文本中的某一段和批量在多个文件中的某行下添加多行代码

需求:

要求在多个yml文件中修改terminationGracePeriodSeconds: 30为terminationGracePeriodSeconds: 360

批量替换文本中的某一段脚本如下:

[root@VM-1-31-centos shell]# cat for.sh
#!/bin/bash
for i in `cat list.txt`
do
  sed -i "s/terminationGracePeriodSeconds: 30/terminationGracePeriodSeconds: 360/g" $i/$i.yml
done

[root@VM-1-31-centos shell]# cat list.txt
a
b
c
这里的场景是目录和目录下的文件名是一样的,文件名后缀为.ynl

批量在多个文件中的某行下添加多行代码,添加代码如下:

[root@VM-1-31-centos shell]# cat 11.txt
          lifecycle:
            preStop:
              exec:
                command:
                - sleep
                - 60s
###值得注意的是缩进的问题

执行脚本如下:

[root@VM-1-31-centos shell]# cat pi.sh
#!/bin/bash
for i in `cat list.txt`
do
  sed -i '/imagePullPolicy\: Always/r 11.txt' $i/$i.yml
done

list文件

list文件如下:
[root@VM-1-31-centos shell]# cat list.txt
a
b
c
这里的场景是目录和目录下的文件名是一样的,文件名后缀为.ynl

 

posted @ 2021-08-26 16:25  B_en′Pǎo  阅读(373)  评论(0)    收藏  举报