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*

3
  • 6
    If your input has a huge number of lines that come in fast, you can speed this up by only updating the count every 10 lines (NR % 10 == 0 { printf ...}), and printing the exact count at the end. Even more fancy would be to print an update when a line comes in only if it's been 100 ms since the last print, maybe with an if() inside the rule. But +1, this is a good simple starting point that's sufficient for some use-cases, like commands that produce lines somewhat slowly, or if terminal updates aren't a bottleneck. Commented Nov 2, 2022 at 19:19
  • 1
    I’ve added those variants to the answer, thanks. AWK (even GNU) doesn’t deal with sub-second intervals AFAICT, but every second or even every n seconds should be sufficient for a long-running job. Commented Nov 4, 2022 at 10:57
  • Or print a result only when the current line count has increased by say 20%, so it will be fast to begin with and slow down over time. This would be useful for potentially huge inputs where one can't even estimate a reasonable size. Marking each line of output with a timestamp might be useful too. Commented Nov 4, 2022 at 20:09