Grep from A to B

Publish date: Jul 31, 2019
Tags: linux, grep, sed

Grepping multiple lines

I use to grep for multiple lines using grep -A as follows

kubectl describe pod <podname> -n <namespace> | grep -A6 Conditions
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:

It returns 10 lines below the matched word “Environment”.

Get Text Between Two Keywords

Sometimes we want to grep for a words between two keywords.

kubectl describe pod <podname> -n <namespace> | sed -n -e '/Conditions/,/Volumes/p'
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes: