리눅스 sed 여러 줄을 치환

1 개요[ | ]

리눅스 sed 여러 줄을 치환

2 예시 1: css[ | ]

tester@localhost:~$ cat a.css 
.text-lowercase {
  text-transform: lowercase !important;
}

@media print {
  * {
    text-shadow: none !important;
    box-shadow: none !important;
  }
}

.text-uppercase {
  text-transform: uppercase !important;
}
tester@localhost:~$ cat a.css | sed ':a;N;$!ba;s/@media print[^}]\+}[^}]\+}//g'
.text-lowercase {
  text-transform: lowercase !important;
}



.text-uppercase {
  text-transform: uppercase !important;
}


tester@localhost:~$ cat a.css 
.text-lowercase {
  text-transform: lowercase !important;
}

@media print {
  * {
    text-shadow: none !important;
    box-shadow: none !important;
  }
  * {
    text-shadow: none !important;
    box-shadow: none !important;
  }
  * {
    text-shadow: none !important;
    box-shadow: none !important;
  }
}

.text-uppercase {
  text-transform: uppercase !important;
}
tester@localhost:~$ cat a.css | sed ':a;N;$!ba;s/@media print.*}[^}^{]\+}//g'
.text-lowercase {
  text-transform: lowercase !important;
}



.text-uppercase {
  text-transform: uppercase !important;
}

3 예시 2: yaml[ | ]

tester@localhost:~$ cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    env:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"
값 변경
tester@localhost:~$ cat pod.yaml | sed ':a;N;$!ba;s/\(DEMO_GREETING[^"]\+\)"[^"]\+"/\1"lorem ipsum"/'
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    env:
    - name: DEMO_GREETING
      value: "lorem ipsum"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"
행 삭제
tester@localhost:~$ cat pod.yaml | sed ':a;N;$!ba;s/\s\+- name: DEMO_GREETING[^:]\+:[^\n]\+//'
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    env:
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}