Home > Enterprise >  coreutils: print nth line in file, block until it exists
coreutils: print nth line in file, block until it exists

Time:05-04

How would I print the nth line of a file or input, and block until it exists? I want to stick to coreutils.

sed 'NUMq;d' file will quickly give me the nth line, but doesn't block.

tail -f file will block, but doesn't do the other thing.

I should be able to pipe the line to something else eg with a file:

<block-until-line-20-exists> file | <process-line>

or, with input:

tail -n 0 -f file | <block-until-line-20-exists> | <process-line>

CodePudding user response:

Nevermind. I think I answered my own question. tail & sed is the solution after all.

tail -n 0 -f file | sed '20q;d'

Turns out that tail stops blocking after sed completes.

EDIT: depending on how you use this, there is a potential for a time-of-check/time-of-use bug.

  • Related