Say I only need the first 5 lines of an output for logging purposes. I also need to know if and when the log has been truncated.
I am trying to use head to do the job, the seq command below outputs 20 lines that get truncated by head, and I echo a truncating information:
> seq -f 'log line %.0f' 20 | head -n 5 && echo '...Output truncated. Only showing first 5 lines...'
log line 1
log line 2
log line 3
log line 4
log line 5
...Output truncated. Only showing first 5 lines...
But if the seq command outputs less than 5 lines, using the same above construction, I get a wrong "truncated" status:
seq -f ' log line %.0f' 3 | head -n 5 && echo '...Output truncated. Only showing first 5 lines...'
log line 1
log line 2
log line 3
...Output truncated. Only showing first 5 lines...
Is there a way for the head command (or another tool) to tell me if it truncated anything so that I only display the "...truncated..." message when needed?