Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

How can I get some block of text by lines?

I have a log file with 6000000 lines and I want get just a block of 607366 to 700000.

I've tried something like:

head -n 607366 | tail -700000 server.log > outputFile.txt
share|improve this question
up vote 8 down vote accepted

You can use sed:

sed -n 607366,700000p server.log > outputFile.txt

If you want to use head and tail, this is the right way:

head -n 700000 server.log | tail -n $(echo 700000-607366+1 | bc) > outputFile.txt

or, shorter:

head -n 700000 server.log | tail -n 92635 > outputFile.txt
share|improve this answer
Thanks for your answer, but the output doesn't started at line 607366 :/ – Emanuella Ramos Cavalcante Oct 11 '13 at 13:41
@EmanuellaRamosCavalcante Please check again; I'm sure that it start from there :/ – Radu Rădeanu Oct 11 '13 at 13:43
@RaduRădeanu maybe you can help with a similar question here:… – JohnMerlino Oct 11 '13 at 14:28
Thanks, sed worked for me! :) – Emanuella Ramos Cavalcante Oct 11 '13 at 14:54

Optimisations on Radu's ones:

sed '607366,$!d;700000q' server.log > outputFile.txt

That way, we stop reading (q) server.log after we find the 700000th line.

head -n 700000 server.log | tail -n "$((700000-607366+1))"

No need to invoke bc here, we can use standard arithmetic expansion.

But doing it the other way round is going to be a lot more efficient:

tail -n +607366 | head -n "$((700000-607366+1))"

as it doesn't involve storing in memory a large number of lines.

share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.