My script accepts a stream on stdin. I want to pass the first line through to stdout no matter what, and grep the remaining lines with -v
and also pass those out to stdout.
I worked out a solution using tee, but I'm wondering if this is guaranteed to always print the output of head
before the output of grep
? What if head
was replaced with something that blocked for 20 minutes before printing anything, would that output appear at the end of stdout after the output of grep
?
tee >(head -n 1) >(tail -n +2 | grep -v -E "$PATTERN")
If the order is not guaranteed, what is the right way of doing this?
head
only outputs one line, it's mostly likely reading blocks larger than one byte in order to find that first newline, and so will probably consume more than just the first line. – Osborn