This is like if I told you to raise your hand as soon as I utter the fourth from last word I am about to say. I am not going to tell you beforehand how many words I am about to say.
A pipe is a stream. Its data has no size, it only has operations for getting the next element from it and / or inserting an element into it, and the result is either a piece of data or a signal that there is no more data.
So unless you first retrieve all the data from the stream, place it into a buffer, count its length, "rewind" the stream, and then retrieve four fewer elements, it can't be done.
EDIT: I need to do more thinking things through instead of coming up with clever analogies:) A stream does not say "stop me immediately n elements before the last", but rather "transmit all elements except the last n", and by maintaining a buffer of just n elements, and waiting until the first n elements have been received before transmitting the first one, it is possible. Obviously this won't work in situations like telecommunications where you want data to be sent immediately after being received as you could if you wanted the first n elements. And I assume truncate
doesn't do it this way.
(attempted downvoting self -1)
1
cat input.txt | some-process
is better written assome_process < input.txt
. – Benoit – 2012-02-24T09:53:29.703