How can I get a part of text file by terminal
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
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
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.