⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06_23.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>Recipe 6.22. Program: tcgrep (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:35:09Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch06_01.htm"TITLE="6. Pattern Matching"><LINKREL="prev"HREF="ch06_22.htm"TITLE="6.21. Program: urlify"><LINKREL="next"HREF="ch06_24.htm"TITLE="6.23. Regular Expression Grabbag"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_22.htm"TITLE="6.21. Program: urlify"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 6.21. Program: urlify"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch06_01.htm"TITLE="6. Pattern Matching"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_24.htm"TITLE="6.23. Regular Expression Grabbag"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 6.23. Regular Expression Grabbag"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch06-65583">6.22. Program: tcgrep</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch06-idx-1000007735-0"></A><ACLASS="indexterm"NAME="ch06-idx-1000007735-1"></A><ACLASS="indexterm"NAME="ch06-idx-1000007735-2"></A><ACLASS="indexterm"NAME="ch06-idx-1000007735-3"></A><ACLASS="indexterm"NAME="ch06-idx-1000007735-4"></A>This program is a Perl rewrite of the Unix <EMCLASS="emphasis">grep</EM> program. Although it runs slower than C versions (especially the GNU <EMCLASS="emphasis">grep</EM>s), it offers many more features.</P><PCLASS="para">The first, and perhaps most important, feature is that it runs anywhere Perl does. Other enhancements are that it can ignore anything that's not a plain text file, automatically expand compressed or <EMCLASS="emphasis">gzip </EM>ped files, recurse down directories, search complete paragraphs or user-defined records, look in younger files before older ones, and add underlining or highlighting of matches. It also supports both the <BCLASS="emphasis.bold">-c</B> option to indicate a count of matching records as well as <BCLASS="emphasis.bold">-C</B> for a count of matching patterns when there could be more than one per record.</P><PCLASS="para">This program uses <EMCLASS="emphasis">gzcat</EM> or <EMCLASS="emphasis">zcat</EM> to decompress compressed files, so this feature is unavailable on systems without these programs and systems without the ability to run external programs (such as the Macintosh).</P><PCLASS="para">Run the program with no arguments for a usage message (see the <CODECLASS="literal">usage</CODE> subroutine in the following code). This command line recursively and case-insensitively greps every file in <EMCLASS="emphasis">~/mail</EM> for mail messages from someone called "kate", reporting the filenames that contained matches.</P><PRECLASS="programlisting">% tcgrep -ril '^From: .*kate' ~/mail</PRE><PCLASS="para">The program is shown in <ACLASS="xref"HREF="ch06_23.htm#ch06-11977"TITLE="tcgrep">Example 6.14</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch06-11977">Example 6.14: tcgrep</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# tcgrep: tom christiansen's rewrite of grep# v1.0: Thu Sep 30 16:24:43 MDT 1993# v1.1: Fri Oct  1 08:33:43 MDT 1993# v1.2: Fri Jul 26 13:37:02 CDT 1996# v1.3: Sat Aug 30 14:21:47 CDT 1997# v1.4: Mon May 18 16:17:48 EDT 1998use strict;                                  # globalsuse vars qw($Me $Errors $Grand_Total $Mult %Compress $Matches);my ($matcher, $opt);              # matcher - anon. sub to check for matches                                  # opt - ref to hash w/ command line optionsinit();                           # initialize globals($opt, $matcher) = parse_args();  # get command line options and patternsmatchfile($opt, $matcher, @ARGV); # process filesexit(2) if $Errors;exit(0) if $Grand_Total;exit(1);###################################sub init {    ($Me = $0) =~ s!.*/!!;        # get basename of program, &quot;tcgrep&quot;    $Errors = $Grand_Total = 0;   # initialize global counters    $Mult = &quot;&quot;;                   # flag for multiple files in @ARGV    $| = 1;                       # autoflush output    %Compress = (                 # file extensions and program names        z  =&gt; 'gzcat',            # for uncompressing        gz =&gt; 'gzcat',        Z  =&gt; 'zcat',    );}###################################sub usage {        die &lt;&lt;EOFusage: $Me [flags] [files]Standard grep options:        i   case insensitive        n   number lines        c   give count of lines matching        C   ditto, but &gt;1 match per line possible        w   word boundaries only        s   silent mode        x   exact matches only        v   invert search sense (lines that DON'T match)        h   hide filenames        e   expression (for exprs beginning with -)        f   file with expressions        l   list filenames matchingSpecials:        1   1 match per file        H   highlight matches        u   underline matches        r   recursive on directories or dot if none        t   process directories in 'ls -t' order        p   paragraph mode (default: line mode)        P   ditto, but specify separator, e.g. -P '%%\\n'        a   all files, not just plain text files        q   quiet about failed file and dir opens        T   trace files as openedMay use a TCGREP environment variable to set default options.EOF}###################################sub parse_args {    use Getopt::Std;    my ($optstring, $zeros, $nulls, %opt, $pattern, @patterns, $match_code);    my ($SO, $SE);    if ($_ = $ENV{TCGREP}) {      # get envariable TCGREP        s/^([^\-])/-$1/;          # add leading - if missing        unshift(@ARGV, $_);       # add TCGREP opt string to @ARGV    }    $optstring = &quot;incCwsxvhe:f:l1HurtpP:aqT&quot;;    $zeros = 'inCwxvhelut';       # options to init to 0 (prevent warnings)    $nulls = 'pP';                # options to init to &quot;&quot; (prevent warnings)    @opt{ split //, $zeros } = ( 0 )  x length($zeros);    @opt{ split //, $nulls } = ( '' ) x length($nulls);    getopts($optstring, \%opt)              or usage();    if ($opt{f}) {                # -f patfile        open(PATFILE, $opt{f})          or die qq($Me: Can't open '$opt{f}': $!);                                  # make sure each pattern in file is valid        while ( defined($pattern = &lt;PATFILE&gt;) ) {            chomp $pattern;            eval { 'foo' =~ /$pattern/, 1 } or                die &quot;$Me: $opt{f}:$.: bad pattern: $@&quot;;            push @patterns, $pattern;        }        close PATFILE;    }    else {                        # make sure pattern is valid        $pattern = $opt{e} || shift(@ARGV) || usage();        eval { 'foo' =~ /$pattern/, 1 } or            die &quot;$Me: bad pattern: $@&quot;;        @patterns = ($pattern);    }    if ($opt{H} || $opt{u}) {     # highlight or underline        my $term = $ENV{TERM} || 'vt100';        my $terminal;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -