OR with a POSIX sed :
Say, for example, I was reading an input of 20 lines and I wanted to strip the first 3 and the last 7. If I resolved to do so w/ sed. I would do it with a tail buffer. I would first add together three and seven for a total strip count of ten and then do:
That is an example which strips the first 3 and last 7 lines from input. The idea is that you can buffer as many lines as you wish to strip from the tail of input in the pattern space on a stack but only P rint the first of these for every line pulled in.
- On lines 1,10 sed P rints nothing because for each of those it is stacking input in pattern space line-by-line in a b ranch loop.
- On the 3rd line all of sed 's stack is d eleted - and so the first 3 lines are stripped from output in one fell swoop.
- When sed reaches the
$ last line of input and attempts to pull in the N ext it hits EOF and stops processing entirely. But at that time pattern space contains all of lines 14,20 - none of which have yet been P rinted, and never are.
- On every other line sed P rints only up to the first occurring \n ewline in pattern space, and D eletes same before beginning a new cycle with what remains - or the next 6 lines of input. The 7th line is appended again to the stack with the N ext command in the new cycle.
And so, of seq 's output (which is 20 sequentially numbered lines). sed only prints:
This gets to be problematic when the number of lines you desire to strip from the tail of input is large - because sed 's performance is directly proportional to the size of its pattern space. Still, though, it is a viable solution in many cases - and POSIX specs a sed pattern space to handle at least 4kb before busting.