Hello nhdw
The exact method for achieving this may vary slightly depending on
which version of "grep" you are using, but for now I will assume you
are using GNU grep (usually the standard one on GNU/Linux systems and
available for nearly every other Unix variant). To find out what
version of grep is available you can run it as
grep -V
In my case it says "grep (GNU grep) 2.4.2" and I'll quote from its man
pages:
OPTIONS
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching
lines.
...
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching
lines.
...
And using those two options alone we should now be able to achieve
what you require:
grep -A 0 -B 2 lazy filename
In other words, search the file specified by "filename" for the text
"lazy", displaying 0 lines after the one that matches, and 2
beforehand.
I've tested this by creating a small file containing the lines you
mentioned in your question along with a couple of others both before
and after, Sure enough, using the above grep command returns the exact
output you wanted.
If any of the above isn't clear, or if you're using a different
version of grep and this technique doesn't seem to work, please let me
know via the "Request Clarification" feature and I'll do my best to
help.
Regards
iaint-ga
Search strategy:
None per se, I just read through the man page for grep |
Request for Answer Clarification by
nhdw-ga
on
20 Nov 2002 01:57 PST
I am using a different version of grep ... As I mentioned, I'm using
the standard unix (Solaris) commands... grep -V does not return the
version number on the particular version I am using, unfortunately ...
Nothing's mentioned in the man page either, as far as which version it
is ...
Unfortunately, my version does not support the -A or -B switches...
|
Request for Answer Clarification by
nhdw-ga
on
20 Nov 2002 02:13 PST
rbnn-ga...
Good question! ...
I would like to see this result for the text you supplied:
x
xx
xx
abc
x
Explination:
x (1st line of your file) would be the first match, but since there's
no lines above it, it only returns the x
Here's where it gets tricky...
The 1st xx in your file would return itself and 2 lines above, which
overlap with the previous match, so this would be skipped, and nothing
printed.
The 2nd xx in your file would return itself and 2 lines above it.
The 3rd xx in your file would return itself and 2 lines above, which
overlaps with the previous match, so this would be skipped, and
nothing printed.
The last x in your file would be matched and would return the blank
line (2 lines above it), abc, and x ...
I had my doubts about this being easy :-)
|
Clarification of Answer by
iaint-ga
on
20 Nov 2002 08:36 PST
Hello again
Unfortunately I have been unable to devise a way to do what you
require using the standard utilities that come with Solaris. It's
worth bearing in mind that Gnu utilities, including grep, are
available in both source code and binary format for Solaris so if you
have the ability to install these (even as a normal user in your home
directory) they might drastically help your text processing
capabilities.
Alternatively it might be possible to produce a script to do this with
awk, however it's a utility and language with which I'm quite
unfamiliar. The final option, and one which I would be able to help
you with, is to create a Perl script for this. However I believe that
Perl, like the GNU textutils, isn't a standard part of the Solaris
install and so this solution doesn't quite fall within the bounds of
your original question.
If none of the above solutions are satisfactory I can only apologise,
and suggest that you email answers-editors@google.com asking for them
to reject this question and then another researcher will be able to
take it on.
Regards
iaint-ga
|