Skip to main content
1 of 3
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

A note of warning:

When you do:

cmd | head

and if the output is truncated, that could cause cmd to be killed by a SIGPIPE, if it writes more lines after head has exited. If it's not what you want, if you want cmd to keep running afterwards, even if its output is discarded, you'd need to read but discard the remaining lines instead of exiting after 10 lines have been output (for instance, with sed '1,10!d' or awk 'NR<=10' instead of head).

So, for the two different approaches:

output truncated, cmd may be killed

cmd | awk 'NR>5 {print "TRUNCATED"; exit}; {print}'
cmd | sed  '6{s/.*/TRUNCATED/;q;}'

output truncated, the rest discarded so cmd is not killed

cmd | awk 'NR<=5; NR==6{print "TRUNCATED"}'
cmd | sed '1,6!d;6s/.*/TRUNCATED/'
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k