Home > other >  how to show n lines above and below my grep search?
how to show n lines above and below my grep search?

Time:02-10

I checked out the manifest file of k8s, but i want to also see the above n lines of my search.

   cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep -i authorization -A4 
        - --authorization-mode=Node,RBAC
        - --client-ca-file=/etc/kubernetes/pki/ca.crt
        - --enable-admission-plugins=NodeRestriction
        - --enable-bootstrap-token-auth=true
        - --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt

how tod this?

CodePudding user response:

Append -B<n> to your command. Example: grep -i authorization -A4 -B4

CodePudding user response:

With -C you get the context around the match:

grep -C4 -i authorization /etc/kubernetes/manifests/kube-apiserver.yaml

From grep man page:

   Context Line Control
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.
              With the -o or --only-matching option, this has no effect and a warning is given.

       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines.  Places a line containing a group separator (described under --group-separator) between contiguous groups of matches.
              With the -o or --only-matching option, this has no effect and a warning is given.

       -C NUM, -NUM, --context=NUM
              Print  NUM  lines  of  output  context.   Places  a line containing a group separator (described under --group-separator) between contiguous groups of matches.  With the -o or
              --only-matching option, this has no effect and a warning is given.

  • Related