SUMMARY: searching for a string

From: SGauthier@domainpharma.com
Date: Mon Jul 10 2000 - 12:52:29 CDT


Why did I forget to check GNU's version of grep....

Here's the basic answer taken from Christophe Colle message:

>
>use gnu grep ... it has the -A and -B switches you can use to
>implement that behavior... gnu grep can be found as a package from
>http://smc.vnet.net or you can compile it from any decent gnu mirror
>

Also added by others (many others) was the -C option and the -# shortcut.

In case anyone wanted the Perl code, Karl Vogel shared a nifty script:
#!/usr/local/bin/perl
# Usage: cgrep [-lines] pattern [files]

$context = 3;

# They might want more or less context.

if ($ARGV[0] =~ /^-(\d+)$/) {
    $context = $1;
    shift;
}

# Get the pattern and protect the delimiter.

$pat = shift;
$pat =~ s#/#\\/#g;

# First line of input will be middle of array.
# In the eval below, it will be $ary[$context].

$_ = <>;
push(@ary,$_);

# Add blank lines before, more input after first line.

for (1 .. $context) {
    unshift(@ary,'');
    $_ = <>;
    push(@ary,$_) if $_;
}

# Now use @ary as a silo, shifting and pushing.

eval <<LOOP_END;
    while (\$ary[$context]) {
     if (\$ary[$context] =~ /$pat/) {
         print "------\n" if \$seq++;
         print \@ary,"\n";
     }
     \$_ = <> if \$_;
     shift(\@ary);
     push(\@ary,\$_);
    }
LOOP_END

Thanks to the following:
John Leadeham
Peter Gutmann
Bernhard Sadlowski
Jed Dobson
Thomas Wardman
Gabriel Rosenkoetter
Paul J. Bell
Sun System Admin ???
Bandried ???
Sid Shapiro
John T. Douglass
Kevin P. Inscoe
Christophe Colle
Karl Vogel
John Saalwaechter
Julie L Baumler

----- Forwarded by Steve Gauthier/PHA/Domain on 07/10/00 01:20 PM -----
                                                                                                                                
                    SGauthier@domainpharma.com
                    Sent by: To: <sun-managers@sunmanagers.ececs.uc.edu>
                    owner-sun-managers@sunmanagers.ec cc:
                    ecs.uc.edu Subject: searching for a string
                                                                                                                                
                                                                                                                                
                    07/10/00 12:20 PM
                                                                                                                                
                                                                                                                                

Okay, anyone out there familiar with OpenVMS? If so, you'll remember the
SEARCH command which would allow you to search for occurrences of strings
(just like grep) but also allow you do list X number of lines before and
after the found occurrences.

Anyone know of a command/program or switch to an existing command that will
do that same thing?

I'm not a wiz at C but I can whip the program up in a day or so... just
wanted to check to make sure I wasn't re-inventing the wheel. Seems like
a useful feature to me so I figured it probably already exists in some or
all flavors of UNIX.

If you can't see why it would be a useful feature, take this example which
I end up running into all the time:

You have a software package installed but you can't remember the stupid
name of the package (as if they were named using meaningful names most of
the time). Typically what I do is type 'pkginfo -l > /tmp/pkgs.txt' then
'view /tmp/pkgs.txt' and do a search in vi for the string. But since
the 'pkginfo -l' output is a fairly regular structure, then if I could
search for a string that I think would appear in the output of the 'pkginfo
-l' command and then print out enough lines before the found string to see
the package name, it would be less steps to take to get the desired
results.

Am I making sense? [or did I drink too much last night? - a joke]

                            \\|//
                            (0~0)
------------------------oooO-(_)-Oooo-----------------------------
Steve Gauthier Domain Pharma Corporation
UNIX/VMS/Macintosh Systems Administrator 10 Maguire Road
PHONE: (781) 778 - 3953 Lexington, MA 02421
FAX: (781) 778 - 3800 E-mail: sgauthier@domainpharma.com
______________________________Oooo._______________________________
                       .oooO (___)

S
U BEFORE POSTING please READ the FAQ located at
N ftp://ftp.cs.toronto.edu/pub/jdd/sun-managers/faq
. and the list POLICY statement located at
M ftp://ftp.cs.toronto.edu/pub/jdd/sun-managers/policy
A To submit questions/summaries to this list send your email message to:
N sun-managers@ececs.uc.edu
A To unsubscribe from this list please send an email message to:
G majordomo@sunmanagers.ececs.uc.edu
E and in the BODY type:
R unsubscribe sun-managers
S Or
. unsubscribe sun-managers original@subscription.address
L To view an archive of this list please visit:
I http://www.latech.edu/sunman.html
S
T

S
U BEFORE POSTING please READ the FAQ located at
N ftp://ftp.cs.toronto.edu/pub/jdd/sun-managers/faq
. and the list POLICY statement located at
M ftp://ftp.cs.toronto.edu/pub/jdd/sun-managers/policy
A To submit questions/summaries to this list send your email message to:
N sun-managers@ececs.uc.edu
A To unsubscribe from this list please send an email message to:
G majordomo@sunmanagers.ececs.uc.edu
E and in the BODY type:
R unsubscribe sun-managers
S Or
. unsubscribe sun-managers original@subscription.address
L To view an archive of this list please visit:
I http://www.latech.edu/sunman.html
S
T



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:14:11 CDT