Skip to main content
fix tiny copy/paste issue
Source Link
Gohu
  • 2.2k
  • 1
  • 17
  • 29

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 'line'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?

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 '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?

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?

Tweeted twitter.com/StackUnix/status/976647006717403137
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264
Source Link
Gohu
  • 2.2k
  • 1
  • 17
  • 29

Truncate output after X lines and print message if and only if output was truncated

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 '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?