sed使用记录
一、 插入批量内容
批量修改文件内容一般使用sed,但是在插入多行内容和不同格式的时候就遇到困难了,今天终于知道怎么处理了,记录一下!
在一个文件里包含image: 192.168.30.60的行下插入以下内容。
resources:
requests:
memory: "768Mi"
limits:
memory: "768Mi"
变更前:
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
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"
二、 匹配行的行尾添加记录
将匹配到image: 192.168.1.200:1179后追加后缀
sed '/image: 192.168.1.200:1179/ s/$/:{{build_image_tag}}/' deploy.yml