Search code examples
bashsedkubectl

Read stream as long as pattern matches


What I currently have:

kubectl logs --timestamps jenkins-68bb89fd4d-fdnwj | sed "/2020-05-15/ q"
2020-05-15T21:48:34.356315314Z Running from: /usr/share/jenkins/jenkins.war

What does it do:

It reads logs with timestamps and stops once sed finds a match. In this case I want extract just logs from given range of time, e.g. 2020-05-17 - 2020-05-19. Kubectl provides flag --since-time= that allows to specify start date for logs, however there's no --until-time flag or equivalent. Issue with my current approach is that there's assumption that pattern that I'm looking will occur in logs, which might not be the case. In this specific case, there might be no logs for given day, but there will be logs for day after.

What am I looking for:

I can achieve my goal, if I will read stream as long as pattern matches. In this case, I can have list of dates in bash, e.g.

dates = (2020-05-15 2020-05-16 2020-05-17)

However I don't know if it's possible to specify pattern for sed, so it will quit once pattern stops to match. Can this be achieved with sed? If not, can this be achieved with awk, grep or some other tool?


Solution

  • The command

    awk '/pattern/ { print; next } { exit }'
    

    prints lines matching pattern, quitting upon encountering a line that doesn't contain a match for the pattern. The /pattern/ syntax can be replaced with a complex expression over multiple fields and such.

    /pattern/ { print; next }
    

    means: "if pattern matches, print the record and then finish processing the record; go to the next record."

    { exit }
    

    is an unconditional action, since it's not preceded by a pattern expression. exit is a built-in statement in Awk; if its argument is omitted, the termination status is successful.