做demo测试时 ,kubectl exec 进入pod内部,执行
sed -i 's/\]\[/,/' learn-instances.json
可以success,满足需求,将learn-instances.json中的字符串“][” 替换为“,”
放入yaml文件的shell -c 参数中执行,整条command line需要加双引号(soft quote),双引号中除了$ `两个符号保留原有特性,其他字符功能被关闭。要想保留需要加转义字符。
本字符串's/\]\[/,/' 中“'” 和“\”为特殊字符,需要加转义字符, 变成 \'s/\\]\\[/,/\'
containers:
- name: retrieve-instance-data
image: saas/sun-java8-build:1353
args:
- /bin/sh
- -c
- "while true; do curl -sL ${JENKINS_JOB_URL} -o ${NGINX_BASE_DIR}/regular-learn-instances.json; curl -sL ${PERF_JENKINS_JOB_URL} -o ${NGINX_BASE_DIR}/perf-learn-instances.json; cat ${NGINX_BASE_DIR}/regular-learn-instances.json ${NGINX_BASE_DIR}/perf-learn-instances.json > ${NGINX_BASE_DIR}/learn-instances.json; sed -i \'s/\\]\\[/,/\' ${NGINX_BASE_DIR}/learn-instances.json; sleep 900; done"
总结: sed 功能 本身字符 '' 和\ 为功能字符,必须保留其功能含义,不能被放在双引号中关闭功能。
双引号中的 sed的单引号 需要加转义字符。
双引号中转义字符 需要再加转义字符。
\\] 两层转义 之所以这样写 第一层转义是为了因为放在sed语句的红色的单引号中,第二层转义是为了保留放在最外层绿色的 双引号中的特性。
\' 一层转义,是因为放在最外层绿色的双引号
同理,YAML文件中使用awk一样的道理,注意awk中 {} 的功能符号。