I have an issue where I want to parse through the output from a file and I want to grab the nth occurrence of text in between two patterns preferably using awk or sed
category
1
s
t
done
category
2
n
d
done
category
3
r
d
done
category
4
t
h
done
Let’s just say for this example I want to grab the third occurrence of text in between category and done, essentially the output would be
category
3
r
d
done
This might work for you (GNU sed):
Turn off automatic printing by using the
-noption. Gather up lines betweencategoryanddone. Store a counter in the hold space and when it reaches 3 print the collection in the pattern space and quit.Or if you prefer awk: