From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f8c9b,e5cab0a42c76da9f X-Google-Attributes: gidf8c9b,public X-Google-Thread: fee84,e5cab0a42c76da9f X-Google-Attributes: gidfee84,public From: will@Starbase.NeoSoft.COM (Will Morse) Subject: Re: grep to == vms search/window ?? Date: 1995/08/02 Message-ID: <3vp0ud$rok@Starbase.NeoSoft.COM> X-Deja-AN: 107426079 references: <3vormk$18g@expert.cc.purdue.edu> organization: NeoSoft Internet Services +1 713 968 5800 newsgroups: comp.unix.osf.osf1,comp.unix.questions In article <3vormk$18g@expert.cc.purdue.edu>, The Artist 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 !