Like Thor answer , but a little shorter:
sed -i '' -e $'1,17d;:a\nN;19,25ba\nP;D' file.txt
-i '' tells sed to modify the file in place. (The syntax may be slightly different on your system. Check the manual page.)
If you want to remove the front lines from the front and tail from the end, you will need to use the following numbers:
1,{front}d;:a\nN;{front+2},{front+tail}ba\nP;D
(I put them in braces here, but it's just a pseudo-code. You will have to replace them with actual numbers. Also, it should work with {front+1} , but this is not on my machine (macOS 10.12.4). I think what a mistake.)
I will try to explain how the team works. Here's a human readable version:
1,17d # delete lines 1 ... 17, goto start :a # define label a N # add next line from file to buffer, quit if at end of file 19,25ba # if line number is 19 ... 25, goto start (label a) P # print first line in buffer D # delete first line from buffer, go back to start
First we skip 17 lines. It's simple. The rest is complicated, but basically we keep a buffer of eight lines. We are just starting to print lines when the buffer is full, but we stop printing when we get to the end of the file, so at the end there are still eight lines left in the buffer that we did not print - in other words, we deleted them.
Jona christopher sahnwaldt
source share