Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have i file that contains 100000 line how i can get the lines from line# 5555 to line# 7777 under linux.

Thanks for all.

share|improve this question
@ibrahim, consider accepting Kyle's answer (by ticking the green checkmark on the left) if it helped you – Jonik Jan 12 '10 at 14:17
you may precise if you want to use sed or if other tools are OK. – Manu H Feb 6 '15 at 16:17
A little bit of research throws at least… – sancho.s Dec 13 '15 at 12:40
sed '5555,7777!d' <filename>

This will print lines 5555-7777 of the file inclusively.

Dennis Posted the following which I agree should be faster:

sed '5555,7777p; 7778q' filename

The following evidence that it should be faster:

$ n=1
$ while [[ n -le 100000 ]]; do echo $n >> sedtest2; n=$((n + 1)); done
$ strace -e trace=read -o sed1 sed '5555,7777!d' sedtest2
$ strace -e trace=read -o sed2 sed '5555,7777p; 7778q' sedtest2
$ wc -l sed1
149 sed1
$ wc -l sed2
14 sed1

In Bash only (for fun):

while read line; do 
    if [[ ($n -ge 5555) && ($n -le 7777)  ]]; then 
        echo $line
    elif [[ $n -gt 7777 ]]; then
    n=$(( $n + 1 ))
done < file
share|improve this answer
Thanks ,This work for me – ibrahim Jan 12 '10 at 13:53
I think your $n -gt 3 should be 7777 perhaps? Also, you can do if (( n >= 5555 )) for more "natural" looking numeric comparison operators (and the ability to leave off the dollar sign). And you can do ((n++)). – Dennis Williamson Jan 12 '10 at 15:18

Quitting when you're done can speed things up:

sed -n '5555,7777p; 7778q' input_file
share|improve this answer
+1 , should be faster, updated my post to show why. – Kyle Brandt Jan 12 '10 at 14:19

Either of these should work;

  • sed -n 'startnumber,*endnumber*p'
  • awk 'NR>=startnumber&&NR<=endnumber' file

Great question by the way ;)

share|improve this answer

I found that the sed option did not work on a mysqldump file, I'm guessing due to handling of quoted line feeds or multibyte characters. head and tail slice it using the same line numbers as grep which was what I needed. To get lines $j through $k, you need:

x=$(( $k - $j + 1 ))
tail -n +$j filename | head -${x} 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.