Home > OS >  SED, GREP, AWK, regex do not display content between two patterns until match the end of second patt
SED, GREP, AWK, regex do not display content between two patterns until match the end of second patt

Time:09-21

Example:

demo, demo-setga, Deployment (apps) has changed:
...
-           image: "imagedemo43"
            image: "imagedemo44"
...
demo, demo-app1, Deployment (apps) has been removed:
- # Source: /charts/app1/templates/deploy.yaml
- apiVersion: apps/v1
-       imagePullSecrets:
-         - name: demo-registry
 
demo, demo-app2, Deployment (apps) has changed:
...
-               value: 4.2.0
                value: 4.3.0
...
demo, demo-app4, Deployment (apps) has been added:
-
  apiVersion: apps/v1
  kind: Deployment
  metadata:
      heritage: Helm
        imagePullSecrets:
          - name: demo-registry
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 2.2.0
                value: 4.3.0
...
demo, demo-app5, PodDisruptionBudget (policy) has been added:
-
  apiVersion: policy/v1beta1
  kind: PodDisruptionBudget
  metadata:
        app: app5
        release: demo
../helm_var/test/dev/secrets.yaml.dec
demo, demo-app8, PodDisruptionBudget (policy) has been added:
-
  # Source: test/charts/app5/templates/pdb.yaml
  apiVersion: policy/v1beta1
      matchLabels:
        app: app5
        release: demo
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 4.1.0
                value: 9.3.0
...

Desired Output:

demo, demo-setga, Deployment (apps) has changed:
...
-           image: "imagedemo43"
            image: "imagedemo44"
...
demo, demo-app1, Deployment (apps) has been removed:
- # Source: /charts/app1/templates/deploy.yaml
- apiVersion: apps/v1
-       imagePullSecrets:
-         - name: demo-registry
 
demo, demo-app2, Deployment (apps) has changed:
...
-               value: 4.2.0
                value: 4.3.0
...
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 2.2.0
                value: 4.3.0
...
../helm_var/test/dev/secrets.yaml.dec
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 4.1.0
                value: 9.3.0
...

start the pattern with line: added:

end pattern: not all, just until they stop on the same block.

block example:

demo, demo-app4, Deployment (apps) has been added:
-
  apiVersion: apps/v1
  kind: Deployment
  metadata:
      heritage: Helm
        imagePullSecrets:
          - name: demo-registry

I tried this sed '/added:/,/\ ,\n/d' file, output not desired:

demo, demo-setga, Deployment (apps) has changed:
...
-           image: "imagedemo43991"
            image: "imagedemo44236"
...
demo, demo-app1, Deployment (apps) has been removed:
- # Source: /charts/app1/templates/deploy.yaml
- apiVersion: apps/v1
-       imagePullSecrets:
-         - name: demo-registry
 
demo, demo-app2, Deployment (apps) has changed:
...
-               value: 4.2.0
                value: 4.3.0
...

I tried this sed '/added:/,/\ /d' file, output not desired:

demo, demo-setga, Deployment (apps) has changed:
...
-           image: "imagedemo43991"
            image: "imagedemo44236"
...
demo, demo-app1, Deployment (apps) has been removed:
- # Source: /charts/app1/templates/deploy.yaml
- apiVersion: apps/v1
-       imagePullSecrets:
-         - name: demo-registry
 
demo, demo-app2, Deployment (apps) has changed:
...
-               value: 4.2.0
                value: 4.3.0
...
  kind: Deployment
  metadata:
      heritage: Helm
        imagePullSecrets:
          - name: demo-docker-registry
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 4.2.0
                value: 4.3.0
...
  kind: PodDisruptionBudget
  metadata:
        app: app5
        release: demo
../helm_var/test/dev/secrets.yaml.dec
  apiVersion: policy/v1beta1
      matchLabels:
        app: app5
        release: demo
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 4.2.0
                value: 4.3.0
...

CodePudding user response:

$ awk '/^[^ -]/{f=(/added:/)} !f' file
demo, demo-setga, Deployment (apps) has changed:
...
-           image: "imagedemo43"
            image: "imagedemo44"
...
demo, demo-app1, Deployment (apps) has been removed:
- # Source: /charts/app1/templates/deploy.yaml
- apiVersion: apps/v1
-       imagePullSecrets:
-         - name: demo-registry
 
demo, demo-app2, Deployment (apps) has changed:
...
-               value: 4.2.0
                value: 4.3.0
...
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 2.2.0
                value: 4.3.0
...
../helm_var/test/dev/secrets.yaml.dec
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 4.1.0
                value: 9.3.0
...

CodePudding user response:

This Perl does what you describe:

perl -0777 -pE 's/^.*added:[\s\S]*?(?=^[^ -])//mg' file

Prints:

demo, demo-setga, Deployment (apps) has changed:
...
-           image: "imagedemo43"
            image: "imagedemo44"
...
demo, demo-app1, Deployment (apps) has been removed:
- # Source: /charts/app1/templates/deploy.yaml
- apiVersion: apps/v1
-       imagePullSecrets:
-         - name: demo-registry
 
demo, demo-app2, Deployment (apps) has changed:
...
-               value: 4.2.0
                value: 4.3.0
...
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 2.2.0
                value: 4.3.0
...
../helm_var/test/dev/secrets.yaml.dec
demo, demo-app9, Deployment (apps) has changed:
...
-               value: 4.1.0
                value: 9.3.0
...

Based on THIS regex.

OR, this awk:

awk 'BEGIN{f=1} 
     /added:$/{f=0; next}
     f==0 && /^[^- ]/ {f=1}
     f
' file 
# same output...

Or, if you really need 1 less line:

awk '/added:$/{f=1; next}
     f==1 && /^[^- ]/ {f=0}
     !f
' file
# same...
  • Related