Google Answers Logo
View Question
 
Q: Matching text in unix (with grep or other standard commands) ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: Matching text in unix (with grep or other standard commands)
Category: Computers > Programming
Asked by: nhdw-ga
List Price: $10.00
Posted: 20 Nov 2002 01:06 PST
Expires: 20 Dec 2002 01:06 PST
Question ID: 111137
I need to know how to do the following, using grep, or any standard
unix (Solaris) commands: I want to locate a match in a text file, and
once the match is located, I want to print the 2 lines above the match
as well as the line with the match...

For Example, If we have a text file with the following text:
the quick
red fox
jumped over
the lazy
dog

I want to search for "lazy" and return the following results:
red fox
jumped over
the lazy

The Command "grep -2 lazy <filename>" has come up in some of my
searches, but is NOT the correct answer. The correct answer is a
command or group of commands that will return the line with the match
(like grep does), as well as a designated number of lines above that
line.

Request for Question Clarification by rbnn-ga on 20 Nov 2002 01:15 PST
What output do you want when searching for "x" in this text:

x

xx
xx
xx

abc
x
Answer  
Subject: Re: Matching text in unix (with grep or other standard commands)
Answered By: iaint-ga on 20 Nov 2002 01:32 PST
Rated:5 out of 5 stars
 
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
nhdw-ga rated this answer:5 out of 5 stars
GNU grep worked for the most part... Even though I do have a specific
need to use the native solaris utilities, the question was answered...
Thanks :-)

Comments  
Subject: Re: Matching text in unix (with grep or other standard commands)
From: studboy-ga on 20 Nov 2002 08:23 PST
 
I recommend download GNU grep source
from

ftp://ftp.sunfreeware.com/pub/freeware/SOURCES/grep-2.5.tar.gz

and compile it for your needs.  You can even tailor 
the source code for your needs.

Another alternative is to use a scripting language like Perl
to imitate GNU grep.  But why go through the trouble?
Subject: Re: Matching text in unix (with grep or other standard commands)
From: rbnn-ga on 20 Nov 2002 08:59 PST
 
The standard way to do this in Unix is to use sed

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy