I am having difficulty grabbing a few lines from text file. For instance say I have the following text in a file:
A I have a cat
B I have a dog
C I have a mouse
X I have a monkey
B I have a rat
T I have a cat
C I have a deer
X I have a turkey
I am trying to find all lines that contain the word "cat" and if the sentence has an "A" as the first letter, I'd like to get the next few lines (including the line that matched the pattern "cat") until I encounter the letter "X" as the first letter of a line.
So for instance, the above text file should print out:
A I have a cat
B I have a dog
C I have a mouse
(prints out until it sees the X)
Note: The line "T I have a cat" should not match because even though it has cat it does not start with the letter "A"
I tried searching for help but could not find anything on printing out lines until a certain pattern is matched. The closest I could find was
awk '/cat/ {for(i=0; i<=5; i++) {getline; print}}' filename
but that prints out a certain number of lines. I want it to print out until it sees the next pattern which is an "X"