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?

6
  • 2
    The process will continue after head, it just won't print anymore to the terminal.
    – 123
    Jul 5, 2016 at 7:06
  • 7
    What problem are you actually trying to solve? Jul 5, 2016 at 7:08
  • 2
    What do you mean to suck them? If you just pipe to head, then the remaining output will be dismissed. Jul 5, 2016 at 7: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 5, 2016 at 7:16
  • @cas trap '' PIPE Jul 5, 2016 at 7:17

1 Answer 1

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'
2
  • 14
    I usually just use ... | tee /dev/null | head ... Jul 5, 2016 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 5, 2016 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .