comment 0

Display line X to line Y from huge file on terminal

This technique is very useful specially if you have a very huge file and you only want to display specific line. Of course you can use cat, head or tail. But that wouldn’t be helpful if you want to display the lines in the middle and the file has thousand lines.

The commands we can use to do this tricks are: head pipe with tail or vice versa, awk,ed and sed. Please see example below.

For example we want to show line 1000 to 1010 on file file.txt. Here’s how we do it.

Using tail pipe with head and vice versa.

tail -n+1000 file.txt | head -n10
head -n1010 file.txt | tail -n10
tail -n1001 file.txt | head -n10

Using ed.

ed -s file.txt <<<"1000,1010p"

Using awk

awk 'NR<1000{next}1;NR==1010{exit}' file.txt

Using sed.

sed -n '1000,1010p' file.txt

Of course, these tricks wouldn’t differ much to each other if you only have couple of thousands of lines. But what if you got million lines, you would prefer the much faster solution right? Here’s a bench mark.


  • 100,000,000-line file generated by seq 100000000 > file.txt
  • Reading lines 50,000,000-50,000,010
  • Tests in no particular order
  • real-time as reported by bash’s builtin time
4.373 4.418 4.395 tail -n+50000000 file.txt | head -n10
5.210 5.179 6.181 sed -n '50000000,50000010p;57890010q' file.txt
5.525 5.475 5.488 head -n50000010 file.txt | tail -n10
8.497 8.352 8.438 sed -n '50000000,50000010p' file.txt
22.826 23.154 23.195 tail -n50000001 file.txt | head -n10
25.694 25.908 27.638 ed -s file.txt <<<"50000000,50000010p"
31.348 28.140 30.574 awk 'NR<57890000{next}1;NR==57890010{exit}' file.txt 
51.359 50.919 51.127 awk 'NR >= 57890000 && NR <= 57890010' file.txt

Check this link for more information

Leave a Reply

Your email address will not be published. Required fields are marked *