sed
http://www.gnu.org/software/sed/
https://www.cnblogs.com/moveofgod/p/3540575.html
查找jobs目录下192.168.1.200 替换为 192.168.1.201
sed -i "s/192.168.1.200/192.168.1.201/g" `grep 192.168.1.200 -rl /home/kevin/.jenkins/jobs`
mac下 要加 "" 表示替换原来文件
sed -i "" "s/预发布/测试/g" `grep 预发布 -rl ./`
sed命令的用法
http://blog.51cto.com/13466287/2066532
https://www.runoob.com/linux/linux-comm-sed.html
mac自带的sed和linux表现不一致, 需要安装gnu-sed
1.brew install gnu-sed --with-default-names 如果报错去掉 --with-default-names 2.vi ~/.bash_profile
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
3.source ~/.bash_profile 或者新开窗口,让设置生效
http://superuser.com/questions/307165/newlines-in-sed-on-mac-os-x
把文件内容插入到另一个文件匹配的内容行下
把data.txt中的内容,插入到config.xml文件内的command下一行
sed -i '/<command>/r data.txt' ./jobs/*-1-*/config.xml
问题
sed替换文本中有单引号时,不能转义,而且单引号也不再处理后的文本里的问题
这种情况要把最外面写成单引号,不能写成双引号,如果写成双引号则内容的的单引号会被去掉使用转义的形式也不能保留住单引号,但是使用转义的形式可以保留住双引号
content = 你好'测试内容'世界
cmd = 'sed -i "5i%s" %s' % (content, file_path)
不能写成下面的情况
cmd = "sed -i '5i%s' %s" % (content, file_path)
先处理comman后面内容,<command>替换为加换行的<command>
sed -i 's/<command>/<command>\n/g' ./*/anlian-server-1-*/config.xml
在</hudson.model.StringParameterDefinition>标签后面添加指定文件内容
sed -i '/<\/hudson.model.StringParameterDefinition>/r param.txt' ./*/anlian-server-1-*/config.xml
在<command>标签后面添加指定文件内容
sed -i '/<command>/r data.txt' ./*/anlian-server-1-*/config.xml
check_branch_name='import'
if [[ -n ${is_check_branch} && ${is_check_branch} == true ]];then
git_code=`git log origin/${check_branch_name} --pretty='%H' -n1`
result=`git log --pretty='%H'| grep ${git_code}`
if [ -z ${result} ]; then
echo "请先从${check_branch_name}分支更新代码"
exit -1
else
echo ${result}
fi
fi
<hudson.model.BooleanParameterDefinition>
<name>is_check_branch</name>
<description>检查是否合过dev分支</description>
<defaultValue>true</defaultValue>
</hudson.model.BooleanParameterDefinition>
beta上执行
sed -i 's/<command>/<command>\n/g' ./jobs/*/config.xml
sed -i '/<command>/r data_beta.txt' ./jobs/*-1-*/config.xml
sed -i '/<command>/r data_beta.txt' ./jobs/*-fast-*/config.xml
sed -i '/<\/hudson.model.StringParameterDefinition>/r param_beta.txt' ./jobs/*-1-*/config.xml
sed -i '/<\/hudson.model.StringParameterDefinition>/r param_beta.txt' ./jobs/*-fast-*/config.xml
test上执行
sed -i 's/<command>/<command>\n/g' ./jobs/*/config.xml
sed -i '/<command>/r data.txt' ./jobs/*-1-*/config.xml
sed -i '/<command>/r data.txt' ./jobs/*-fast-*/config.xml
sed -i '/<\/hudson.model.StringParameterDefinition>/r param.txt' ./jobs/*-1-*/config.xml
sed -i '/<\/hudson.model.StringParameterDefinition>/r param.txt' ./jobs/*-fast-*/config.xml