What's the best way to view lines X through Y of a large file?

1

I have a very large text file and I want to view, say, lines 2000 through 2010 (with the line numbers included)

I know one sort of roundabout way of getting there:

sc -l [file]
cat -n [file] | tail -n [previous result - 2000] | head -n 10

But it feels like there must be a better way. Is there?

Dan Tao

Posted 2012-12-07T18:41:18.493

Reputation: 999

1Your last command can be simplified to head -2010 FileName | tail -10. – amit_g – 2012-12-07T19:06:36.113

@amit_g: Ha, good call. Somehow I totally missed that one. – Dan Tao – 2012-12-07T19:13:26.153

Answers

4

You could use sed if you know the lines you want.

sed -n X,Yp file.txt

Or if it's stuff between some REGEX, with awk:

awk '/FIRST REGEX/,/LAST REGEX/' input.txt

Or an awk way of doing the sed suggestion:

awk 'NR>=X && NR<=Y' file.txt

nerdwaller

Posted 2012-12-07T18:41:18.493

Reputation: 13 366

+1 sed is a good choice. It can do regex ranges as well, e.g., sed -n '/^#ifdef/,/^#endif/p'. – Nicole Hamilton – 2012-12-07T19:23:42.500