This is going to be a very brief post on how to the read lines that come directly before and after a piped grep command.

Let me being by saying this:

Grep has become my new best friend over the course of the last few years.

I am currently learning about the Istio Service Mesh in Kubernetes (here and here) and I wanted to quickly view some information about the istio-ingressgateway which (in my current working environment) resides within the very long istio-1.0.6/install/kubernetes/istio-demo.yaml  manifest file.

Here is a straight forward way of viewing the 10 lines that precede the word LoadBalancer:

cat istio-1.0.6/install/kubernetes/istio-demo.yaml | grep -i -B 10 loadbalancer

..returns:

  name: istio-ingressgateway
  namespace: istio-system
  annotations:
  labels:
    chart: gateways-1.0.6
    release: istio
    heritage: Tiller
    app: istio-ingressgateway
    istio: ingressgateway
spec:
  type: LoadBalancer
  

To view the 10 lines that follow the word LoadBalancer:

cat istio-1.0.6/install/kubernetes/istio-demo.yaml | grep -i -A 10 loadbalancer

..returns:

  type: LoadBalancer
  selector:
    app: istio-ingressgateway
    istio: ingressgateway
  ports:
    -
      name: http2
      nodePort: 31380
      port: 80
      targetPort: 80
    -

Let's go nuts and view the 10 lines that come before AND after the word LoadBalancer:

cat istio-1.0.6/install/kubernetes/istio-demo.yaml | grep -i -B 10 -A 10 loadbalancer

..returns:

  name: istio-ingressgateway
  namespace: istio-system
  annotations:
  labels:
    chart: gateways-1.0.6
    release: istio
    heritage: Tiller
    app: istio-ingressgateway
    istio: ingressgateway
spec:
  type: LoadBalancer
  selector:
    app: istio-ingressgateway
    istio: ingressgateway
  ports:
    -
      name: http2
      nodePort: 31380
      port: 80
      targetPort: 80
    -
    

It's like magic I tell ya!

Never get stuck scrolling through long log or manifest files again when working on a virtual server with Vim and Tmux!!  That's it for now. ...back to learning more about Istio:-)