* grep to == vms search/window ??
@ 1995-08-02 4:00 The Artist
1995-08-02 4:00 ` Will Morse
1995-08-03 4:00 ` Malcolm Beattie
0 siblings, 2 replies; 4+ messages in thread
From: The Artist @ 1995-08-02 4:00 UTC (permalink / raw)
i'm running dec osf v3.2a
i'm trying to make the grep command act similary to
the vms *search foo.bar string/window=(3,3)*. and
ideas? it would allow me to search the file foo.bar
for the string *string*. then, if it finds it,
it would display the line containing the string AND
three lines directly above it, and three lines
directly below it. any help?
thanks
snafu@ncnetv.pprd.abbott.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: grep to == vms search/window ??
1995-08-02 4:00 grep to == vms search/window ?? The Artist
@ 1995-08-02 4:00 ` Will Morse
1995-08-03 4:00 ` Malcolm Beattie
1 sibling, 0 replies; 4+ messages in thread
From: Will Morse @ 1995-08-02 4:00 UTC (permalink / raw)
In article <3vormk$18g@expert.cc.purdue.edu>,
The Artist <snafu@expert.cc.purdue.edu> wrote:
>i'm running dec osf v3.2a
>
>i'm trying to make the grep command act similary to
>the vms *search foo.bar string/window=(3,3)*. and
>ideas? it would allow me to search the file foo.bar
>for the string *string*. then, if it finds it,
>it would display the line containing the string AND
>three lines directly above it, and three lines
>directly below it. any help?
>
>thanks
>snafu@ncnetv.pprd.abbott.com
>
You should get wgrep. I have a perl version of it, as follows:
#! /bin/perl
$before = 3 ;
$after = 3;
$show_stars = 1 ;
$show_nums = 0;
$sep = "*********\n" ;
while (@ARGV[0] =~ /^-([A-z])(.*)/)
{
$arg = $1;
if ($arg eq "s") { $show_stars = 0; }
elsif ($arg eq "n") { $show_nums = 1; }
elsif ($arg eq "w")
{
split(/:/,$2);
$before = @_[0] if @_[0] ne '';
$after = @_[1] if @_[1] ne '';
}
else
{
&usage("wgrep: invalid option: $ARGV[0]");
}
shift;
}
&usage("missing regular expression") if ! @ARGV[0];
$regexp = @ARGV[0];
print "Searching for ",$regexp ;
shift;
$regexp =~ s,/,\\/,g;
if (! @ARGV[0]) { @ARGV[0] = "STDIN"; }
FILELOOP:
foreach $file (@ARGV)
{
print " in $file \n";
if ($file ne "STDIN" && ! open(NEWFILE,$file))
{
print STDERR "Can't open file $file; skipping it. \n";
next FILELOOP;
}
$lnum = "000000";
$nbef = 0;
$naft = 0;
$matched = 0;
$matched2 = 0;
&clear_buf(0) if $before > 0;
$fhandle = $file eq "STDIN" ? STDIN : NEWFILE;
while (<$fhandle>)
{
++$lnum;
if ($matched)
{
if ($_ =~ /$regexp/)
{
$naft = 0;
&print_info(1);
print $_;
}
else
{
if ($after > 0 && ++$naft <= $after )
{
&print_info(0);
print $_;
}
else
{
$matched = 0;
$naft = 0;
push(@line_buf, $_);
$nbef++ ;
}
}
}
else
{
if ($_ =~ /$regexp/)
{
$matched = 1;
print $sep if $matched2 && $nbef > $before ;
print "******** file = $file ********\n"
if ! $matched2++;
&clear_buf(1) if $before > 0;
$nbef = 0 ;
&print_info(1);
print $_;
}
elsif ($before > 0)
{
shift (@line_buf) if $nbef >= $before;
push (@line_buf, $_);
$nbef++;
}
}
}
}
exit;
sub usage {
print STDERR @_[0],"\n" if @_[0];
print STDERR " usage: wgrep [-n] [-w[b][:a]] [-s] regexp [files] \n";
print STDERR " option: -n, number lines, \n";
print STDERR " option: -w3:4 print three line before and four after\n";
print STDERR " option: -s turn OFF star in front of match lines \n";
exit;
}
sub clear_buf {
$print_flag = @_[0];
$i = 0;
$j = 0;
if ($print_flag)
{
if ($shownums)
{
$target = $lnum - ($#line_buf + 1);
$lnum = "000000";
while ($i++ < $target) { ++$lnum; }
}
while ($j <= $#line_buf)
{
&print_info(0);
print @line_buf[$j++];
$lnum++ if $show_nums;
}
}
@line_buf = ();
}
sub print_info {
print @_[0] ? "* " : " " if $show_stars;
print $lnum," " if $show_nums;
}
Will
--
# Gravity, # Will Morse
# not just a good idea, # BHP Petroleum (Americas) Inc.
# it's the law. # Houston, Texas
# # will@starbase.neosoft.com
#
# These are my views and do not necessarly reflect the views of BHP !
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: grep to == vms search/window ??
1995-08-02 4:00 grep to == vms search/window ?? The Artist
1995-08-02 4:00 ` Will Morse
@ 1995-08-03 4:00 ` Malcolm Beattie
1995-08-10 4:00 ` Ultrix==BSD/dUnix==SysV (was :grep to == vms search/window ??) Alan Davis
1 sibling, 1 reply; 4+ messages in thread
From: Malcolm Beattie @ 1995-08-03 4:00 UTC (permalink / raw)
In article <3vormk$18g@expert.cc.purdue.edu>,
The Artist <snafu@expert.cc.purdue.edu> wrote:
>i'm running dec osf v3.2a
>
>i'm trying to make the grep command act similary to
>the vms *search foo.bar string/window=(3,3)*. and
>ideas? it would allow me to search the file foo.bar
>for the string *string*. then, if it finds it,
>it would display the line containing the string AND
>three lines directly above it, and three lines
>directly below it. any help?
Use GNU grep instead: the option -3 displays 3 lines either side of
each match. If you want an asymmetric window then you can use, e.g.,
-A 4 -B 5 to display 4 lines after and 5 lines before each match.
You'll also find that in its "fixed string" incarnation, it doesn't
have the same bugs as the fgrep supplied in OSF/1 3.0. It might be
fixed in 3.2c but I doubt it.
% echo abacababacababc | fgrep abacababc
% echo abaabaac | fgrep abaac
%
(actually these and similar bugs are present in most BSD and SysV
fgreps but, interestingly, they have different bugs. OSF/1 shows
the same bug as the SysV fgrep whereas Ultrix shows the same bug
as the BSD one.)
--Malcolm
--
Malcolm Beattie <mbeattie@sable.ox.ac.uk>
Oxford University Computing Services
"Widget. It's got a widget. A lovely widget. A widget it has got." --Jack Dee
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Ultrix==BSD/dUnix==SysV (was :grep to == vms search/window ??)
1995-08-03 4:00 ` Malcolm Beattie
@ 1995-08-10 4:00 ` Alan Davis
0 siblings, 0 replies; 4+ messages in thread
From: Alan Davis @ 1995-08-10 4:00 UTC (permalink / raw)
mbeattie@sable.ox.ac.uk (Malcolm Beattie) wrote:
..
>(actually these and similar bugs are present in most BSD and SysV
>fgreps but, interestingly, they have different bugs. OSF/1 shows
>the same bug as the SysV fgrep whereas Ultrix shows the same bug
>as the BSD one.)
That's not all that surprising : Ultrix is BSD-like with extensions,
OSF/1 is SysV-like with extensions - Now Digital Unix, ie it passes the
qualification tests to be called Unix as defined by the standard, which
is SysV based.
--
Alan Davis Digital Equipment Corp. | Employed by, but
System Support Specialist 5555 Windward Parkway | not speaking for,
davis@alf.dec.com Alpharetta, GA 30201 | Digital Equipment Corp
---------- Where am I going and what am I doing in this handbasket? ----------
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1995-08-10 4:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-08-02 4:00 grep to == vms search/window ?? The Artist
1995-08-02 4:00 ` Will Morse
1995-08-03 4:00 ` Malcolm Beattie
1995-08-10 4:00 ` Ultrix==BSD/dUnix==SysV (was :grep to == vms search/window ??) Alan Davis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox