1、 插入批量內容web
批量修改文件內容通常使用sed,可是在插入多行內容和不一樣格式的時候就遇到困難了,今天終於知道怎麼處理了,記錄一下!api
在一個文件裏包含image: 192.168.30.60的行下插入如下內容。app
resources:ui
requests:spa
memory: "768Mi"requests
limits:it
memory: "768Mi"io
變動前:class
apiVersion: extensions/v1beta1test
kind: Deployment
metadata:
name: bi-web
spec:
replicas: 1
template:
metadata:
labels:
app: bi-web
spec:
containers:
- name: bi-web
image: 192.168.30.60:1179/test100/bi-web
ports:
- containerPort: 8080
命令:
sed -i '/image: 192.168.30.60/a\ resources:\n requests:\n memory: "768Mi"\n limits:\n memory: "768Mi"' */deploy.yml
變動後:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: bi-web
spec:
replicas: 1
template:
metadata:
labels:
app: bi-web
spec:
containers:
- name: bi-web
image: 192.168.30.60:1179/test100/bi-web
resources:
requests:
memory: "768Mi"
limits:
memory: "768Mi"
2、 匹配行的行尾添加記錄
將匹配到image: 192.168.1.200:1179後追加後綴
sed '/image: 192.168.1.200:1179/ s/$/:{{build_image_tag}}/' deploy.yml