05-29-2008
3,653,
12
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks Given: 0
Thanked 12 Times in 10 Posts
Without more specifics, it's hard to give a good answer.
If each line in the file is unique, you can remember the last line you saw, and only read lines after that line. For example, in a log file where every line has a date and a time stamp, this is trivially true. Or, following your own suggestion, you can copy the file and later run diff or something to remove the lines you have already copied (and thus already processed). This sounds rather heavy, though. Finally, if this is a log file, it's probably being rotated nightly or weekly, or at least monthly; so you should perhaps at least take care to notice when the file shrinks; or maybe you could even try to piggyback your grepping on the process which does the rotation, if you can (probably requires root access).
Grabbing more than one line again depends heavily on file format etc, but if the middle one of those lines is the one you grep for, a simple context argument grep -C 1 pattern file will get you one line before, and one line after the match. Some versions of grep also have -B and -A options to control how many lines before and after the match to include, respectively. If you don't have those options, or need more elaborate processing, a sed or awk script is probably the way to go.