14

I need a utility that will print the first n lines, but then continue to run, sucking up the rest of the lines, but not printing them. I use it to not overwhelm the terminal with the output of a process that needs to continue to run (it writes results to a file).

I figured I can do process | {head -n 100; cat > /dev/null}, but is there something more elegant?

IttayD
  • 381
  • 2
    The process will continue after head, it just won't print anymore to the terminal. – 123 Jul 05 '16 at 07:06
  • 7
    What problem are you actually trying to solve? – Satō Katsura Jul 05 '16 at 07:08
  • 2
    What do you mean to suck them? If you just pipe to head, then the remaining output will be dismissed. – Julie Pelletier Jul 05 '16 at 07:10
  • 7
    @JuliePelletier and stdout will be closed and well-written programs (those that only write to stdout, anyway) will notice that and terminate early. – cas Jul 05 '16 at 07:16
  • @cas trap '' PIPE – Satō Katsura Jul 05 '16 at 07:17
  • @123 It may or may not, depending on whether or not the process has a bug. Some programs check for errors when writing to standard output and abort if they detect one. This is sane if, and only if, it is always an error to discard this output. Clearly, the OP is in a case where it is not an error to discard this output. Sadly, some programs do have precisely this bug, and a hack is needed to work around it. – David Schwartz Jul 05 '16 at 20:44

1 Answers1

24

To continue "sucking up" the output from process while only printing the first 100 (or whatever) lines:

process | awk 'NR<=100'

Or:

process | sed -n '1,100p'
John1024
  • 74,655
  • 14
    I usually just use ... | tee /dev/null | head ... – David Schwartz Jul 05 '16 at 16:55
  • 2
    @DavidSchwartz yep - it's what I'd do. Far more preferrable, since you can also dump all the output in a file and examine it at a later point in time. You never know when that's going to be needed - at worse, you have a file sitting around which you occasionally overwrite, at best, you can use it as a log to analyse what/why happened. – VLAZ Jul 05 '16 at 18:10