0

I'm trying to process logs while tailing them.

This command works:

kubectl logs "$pod" --tail=100 | egrep pattern | cut -d ' ' -f 1

But this command produces no output:

kubectl logs "$pod" --tail=100 -f | egrep pattern | cut -d ' ' -f 1

The difference between the two is -f passed to the kubectl logs in the second variant, which follows the the logs instead of exiting.

Strangely enough, this works:

kubectl logs "$pod" --tail=100 -f | egrep pattern

And this works:

kubectl logs "$pod" --tail=100 -f | cut -d ' ' -f 1

It's only when you add the second pipe that it stops working.

What is going on here? I'm guessing this is an output buffering issue, but I'm not sure how to address it.

5
  • Does your egrep support --line-buffered command line option? Try it. Commented Oct 13, 2021 at 17:29
  • @KamilMaciorowski --line-buffered did work. Thanks a lot. Do you know what's going on? Commented Oct 13, 2021 at 17:45
  • My knowledge is in my answer to the linked duplicate. Commented Oct 13, 2021 at 17:49
  • Thanks @KamilMaciorowski. Do you know why adding, say, | xargs echo {} doesn't work though? Commented Oct 13, 2021 at 17:59
  • Before spawning the first echo, your xargs is waiting for as many arguments as the OS will be able to take, or for EOF. There is no EOF, so it's waiting. Commented Oct 13, 2021 at 18:05

0

Browse other questions tagged .