Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • choosing l as the variable name made me squint my eyes on $l, thinking it was $1 ^^ (but as I know (and 100% trust) you, I reread and saw the truth). Just for curiosity: to avoid some "race condition" between the wc -l and the tail -f (if the file grows fast, one may discard some lines and thus the NR starts from the wrong number), is it possible to skip $l lines instead? (and what limit is there to tail's -n in posix & in gnu?). Maybe with a temporary intermediate file? Commented Nov 27, 2017 at 13:10
  • @OlivierDulac, tail -n +1 (read anything from the start position) addresses the race condition concerns. It will read the lines that were not in the file at the time wc -l terminated, from the exact position wc left it. So NR will have the right position regardless of how many lines have been written in between wc ending and tail starting. It's if you told tail to start from some position relative to the end of the file that you'd have issues. Commented Nov 27, 2017 at 13:35
  • oh, interresting: indeed, the data accumulate into stdin while nothing reads it (between the end of the wc until the start of the head)... I should have realised that. Thx. Now I see why you "<file". clever, as usual :) Commented Nov 27, 2017 at 13:45
  • 1
    @OlivierDulac, about limitations (which don't apply to tail -n +1 here), for regular files, most implementations don't have one as they can start from the end and seek back until they find the nth newline without having to store more than one buf worth of data in memory. For non-seekable input, that's where you can run into limits. POSIX requires implementations to be able to store at least 10 x LINE_MAX bytes (LINE_MAX being at least 2048). GNU tail has no limit other than memory AFAIK Commented Nov 27, 2017 at 13:54