Super User is a question and answer site for computer enthusiasts and power users. 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 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?

share|improve this question
1  
Your last command can be simplified to head -2010 FileName | tail -10. – amit_g Dec 7 '12 at 19:06
    
@amit_g: Ha, good call. Somehow I totally missed that one. – Dan Tao Dec 7 '12 at 19:13
up vote 4 down vote accepted

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

share|improve this answer
    
+1 sed is a good choice. It can do regex ranges as well, e.g., sed -n '/^#ifdef/,/^#endif/p'. – Nicole Hamilton Dec 7 '12 at 19:23
    
Thanks @NicoleHamilton, that's great to know! – nerdwaller Dec 7 '12 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .