Last Updated: February 25, 2016
·
779
· gahtune

Print lines in the middle of a big file

I used to head a file and tail the result in order to get the middle of a file

for example:

head -1000 bigfile | tail -20

The disadvantages is that you need to get 1000 lines in order to get 20 lines at the end !

I study a little bit sed commands and found out this:

sed -n '980,1000p' bigfile

This is equivalent and faster ;) (try to time it)

The -n is to not print the non selected lines
and the p stand for print

You also can:

sed -n '980,1000!d' bigfile

To not delete the lines selected. I didn't try to benchmark this solution compare to the previous one, but the action seems bigger (i.e. delete a lot of lines vs select few lines)

1 Response
Add your response

If you need just view the file, probably less +1000 bigfile is faster and more user friendly.

over 1 year ago ·