Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 6 Aug 1996 13:09:15 -0700 (PDT)
From:      Gary Kline <kline@tera.com>
To:        fqueries@jraynard.demon.co.uk (James Raynard)
Cc:        kline@tera.com, questions@freebsd.org
Subject:   Re: ``tgrep'' from Unix Power Tools
Message-ID:  <199608062009.NAA11413@athena.tera.com>
In-Reply-To: <199608061014.KAA01581@jraynard.demon.co.uk> from James Raynard at "Aug 6, 96 10:14:13 am"

next in thread | previous in thread | raw e-mail | index | archive | help
According to James Raynard:
> > 
> >    Has anyone used the short `tgrep' perl script from the UNIX 
> >    POWER TOOLS book?  It works on my Sun and fails with a strange
> >    error blurb on my FreeBSD machine.
> > 
> >    tgrep grep only text file, skipping directories and binaries.
> 
> If it's the same tgrep as the one in the "Camel book", I'm sure it
> ought to work.  Could you post a copy of it and tell us what the
> exact error message is?
> 
> 

     In case people are curious, below are the 40 lines; with debugging:

     ~/bin
#!/usr/local/bin/perl
# Usage: tgrep [-l] pattern [files]
# Handle
if ($ARGV[0] eq '-l') {
    shift;
    $action = <<'EOF';
            print $file,"\n";
            next FILE;
EOF
}
else {
    $action = <<'EOF';
            print $file,":\t", $_;
EOF
}
# Get pattern and protect the delimiter we'll use.
$pat = shift;
$pat =~ s/!/\\!/g;
# Generate the program.
$prog = <<EOF;
FILE: foreach \$file (\@ARGV) {
    open(FILE,\$file) || do {
        print STDERR "Can't open \$file: \$!\\n";
        next;
    };
    next if -d FILE;    # ignore directories
    next if -B FILE;    # ignore binary files
    while (<FILE>) {
        if (m!$pat!) {
            $action
        }
    }
}
EOF
# We often put in lines like this while developing scripts, so we
# can see what program the program is writing.
print $prog if $debugging;
# And finally, do it.
eval $prog;
die $@ if $@;

     From the debug is this:

-T and -B not implemented on filehandles at (eval) line 7.

     and the reason, as Nate Williams explained, is that our version
     of stdio doesn't check for binary file types.

	gary





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199608062009.NAA11413>