I have a large log file I want to review. All the bad stuff starts at the certain occurrence of an error string. I want to then look at the last X lines from that point and see what might have caused that error. I can't open the file with my favourite text editor because it exhausts all the RAM on the machine.

I thought perhaps I might be able to find the line it occurs on and then use another utility to get data from line X to line Y. Is this possible?

3 Answers 3


You can just use "grep" with the -A and/or -B options. The -A switch will read the X number of lines after the error, so it's probably what you want, and the -B switch will read X number of lines before the error, so you would do something like this:

grep -A10 -B2 "string to find" /path/and/file.tofind

to find the 10 lines which occur after "string to find", as well as the 2 lines before it.

Alternatively – and it's probably a much worse solution, you could simply use "head" and "tail" to find the first and then the last part of the file you want; but this assumes you know the line numbers. I.e., if you have a long line file, and you are wanting to read lines 500-510, you might try this:

head -510 /etc/file/to/search | tail -10

Which would first extract the first 510 lines of the file, and then read off the last 10 of those lines.

  • 2
    if not using a regexp pattern, grep -F will give best performance.
    – suspectus
    Sep 24, 2013 at 19:34
  • This is nice, I didn't know about these switches. By the time you answered, I had achieved this by using grep -n to get the line number, and sed '#,#p' to get a line range. If the error string occurs frequently do these switches only return lines for the first occurrence? Can I use m 1 to achieve that if not?
    – deed02392
    Sep 24, 2013 at 21:06

Get a new "favorite editor".  vi does not read files into RAM in their entirety.

In vi you can search for a string by typing /, the string to search for, and Enter.  The matching line will be positioned at the middle of the screen (window), so you will be able to see approximately 12 lines before and 12 lines after (if your window is 24 lines high).  You can, of course, scroll forward or backward to see more.  Press n to find the next occurrence of the string.


The following script will give you the first 10 rows and 10 columns of a bigfile cut -f1-10 -d' ' your_big_file| head -n10

  • 1
    cut -f1-10 -d' ' your_big_file| head -n10 Mar 13, 2019 at 13:43
  • (1) What if ERROR: The foo failed to bar occurs at line 4283 of the file?  How does the user look at lines 4281-4293? (2) Why are you talking about columns?  The question doesn’t say anything about columns. Mar 13, 2019 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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