?? search.pl
字號:
## do checks for -name/-regex/-path tests if ($DO_GLOB_TESTS && !(eval $GLOB_TESTS)) { warn qq/skip (filename): $file\n/ if $WHY; next; } ## If we're not repeating files, ## skip this one if we've done it, or note we're doing it. unless ($DOREP) { if (defined $file_done{$id}) { warn qq/skip (did as "$file_done{$id}"): $file\n/ if $WHY; next; } $file_done{$id} = $file; } if ($DO_MAGIC_TESTS) { if (!open(FILE_IN, $file)) { &clear_message if $VERBOSE && $STDERR_SCREWS_STDOUT; warn qq/$0: can't open: $file\n/; next; } unless (read(FILE_IN, $magic'H, $HEADER_BYTES)) { &clear_message if $VERBOSE && $STDERR_SCREWS_STDOUT; warn qq/$0: can't read from "$file"\n"/; close(FILE_IN); next; } eval $MAGIC_TESTS; if ($magic'val) { close(FILE_IN); warn qq/skip (magic): $file\n/ if $WHY; next; } seek(FILE_IN, 0, 0); ## reset for later <FILE_IN> } if ($WHY != 0 && $VERBOSE > 1) { if ($VERBOSE>2||$vv_print++>$VV_PRINT_COUNT||($vv_size+=-s _)>$VV_SIZE){ &message($file); $vv_print = $vv_size = 0; } } if ($DELAY) { sleep($DELAY); } if ($FIND_ONLY) { &clear_message if $VERBOSE && $STDERR_SCREWS_STDOUT; print $file, "\n"; $retval=0; ## we've found something close(FILE_IN) if $DO_MAGIC_TESTS; next; } else { ## if we weren't doing magic tests, file won't be open yet... if (!$DO_MAGIC_TESTS && !open(FILE_IN, $file)) { &clear_message if $VERBOSE && $STDERR_SCREWS_STDOUT; warn qq/$0: can't open: $file\n/; next; } if ($LIST_ONLY && $CAN_USE_FAST_LISTONLY) { ## ## This is rather complex, but buys us a LOT when we're just ## listing files and not the individual internal lines. ## local($size) = 4096; ## block-size in which to do reads local($nl); ## will point to $_'s ending newline. local($read); ## will be how many bytes read. local($_) = ''; ## Starts out empty local($hold); ## (see below) while (($read = read(FILE_IN,$_,$size,length($_)))||length($_)) { undef @parts; ## if read a full block, but no newline, need to read more. while ($read == $size && ($nl = rindex($_, "\n")) < 0) { push(@parts, $_); ## save that part $read = read(FILE_IN, $_, $size); ## keep trying } ## ## If we had to save parts, must now combine them together. ## adjusting $nl to reflect the now-larger $_. This should ## be a lot more efficient than using any kind of .= in the ## loop above. ## if (@parts) { local($lastlen) = length($_); #only need if $nl >= 0 $_ = join('', @parts, $_); $nl = length($_) - ($lastlen - $nl) if $nl >= 0; } ## ## If we're at the end of the file, then we can use $_ as ## is. Otherwise, we need to remove the final partial-line ## and save it so that it'll be at the beginning of the ## next read (where the rest of the line will be layed in ## right after it). $hold will be what we should save ## until next time. ## if ($read != $size || $nl < 0) { $hold = ''; } else { $hold = substr($_, $nl + 1); substr($_, $nl + 1) = ''; } ## ## Now have a bunch of full lines in $_. Use it. ## if (eval $REGEX_TEST) { &clear_message if $VERBOSE && $STDERR_SCREWS_STDOUT; print $file, "\n"; $retval=0; ## we've found something last; } ## Prepare for next read.... $_ = $hold; } } else { ## else not using faster block scanning..... $lines_printed = 0 if $NICE; while (<FILE_IN>) { study; next unless (eval $REGEX_TEST); ## ## We found a matching line. ## $retval=0; &clear_message if $VERBOSE && $STDERR_SCREWS_STDOUT; if ($LIST_ONLY) { print $file, "\n"; last; } else { ## prepare to print line. if ($NICE && $lines_printed++ == 0) { print '-' x 70, "\n" if $NICE > 1; print $file, ":\n"; } ## ## Print all the prelim stuff. This looks less efficient ## than it needs to be, but that's so that when the eval ## is compiled (and the tests are optimized away), the ## result will be less actual PRINTs than the more natural ## way of doing these tests.... ## if ($NICE) { if ($REPORT_LINENUM) { print " line $.: "; } else { print " "; } } elsif ($REPORT_LINENUM && $PREPEND_FILENAME) { print "$file,:$.: "; } elsif ($PREPEND_FILENAME) { print "$file: "; } elsif ($REPORT_LINENUM) { print "$.: "; } print $_; print "\n" unless m/\n$/; } } print "\n" if ($NICE > 1) && $lines_printed; } close(FILE_IN); } } closedir(DIR);}__END__.00; ## finish .ig 'di \" finish diversion--previous line must be blank.nr nl 0-1 \" fake up transition to first page again.nr % 0 \" start at page 1.\"__________________NORMAL_MAN_PAGE_BELOW_________________.ll+10n.TH search 1 "Dec 17, 1994".SH SEARCHsearch \- search files (a'la grep) in a whole directory tree..SH SYNOPSISsearch [ grep-like and find-like options] [regex ....].SH DESCRIPTION.I Searchis more or less a combo of 'find' and 'grep' (although the regularexpression flavor is that of the perl being used, which is closer toegrep's than grep's)..I Searchdoes generally the same kind of thing that.nf find <blah blah> | xargs egrep <blah blah>.fidoes, but is.I muchmore powerful and efficient (and intuitive, I think).This manual describes.I searchas of version "941227.4". You can always find the latest version at.nf http://www.wg.omron.co.jp/~jfriedl/perl/index.html.fi.SH "QUICK EXAMPLE"Basic use is simple:.nf % search jeff.fiwill search files in the current directory, and all sub directories, forfiles that have "jeff" in them. The lines will be listed with thecontaining file's name prepended..PPIf you list more than one regex, such as with.nf % search jeff Larry Randal+ 'Stoc?k' 'C.*son'.fithen a line containing any of the regexes will be listed.This makes it effectively the same as.nf % search 'jeff|Larry|Randal+|Stoc?k|C.*son'.fiHowever, listing them separately is much more efficient (and is easierto type)..PPNote that in the case of these examples, the.B \-w(list whole-words only) option would be useful..PPNormally, various kinds of files are automatically removed from consideration.If it has has a certain ending (such as ".tar", ".Z", ".o", .etc), or ifthe beginning of the file looks like a binary, it'll be excluded.You can control exactly how this works -- see below. One quick way tooverride this is to use the.B \-alloption, which means to consider all the files that would normally beautomatically excluded.Or, if you're curious, you can use.B \-whyto have notes about what files are skipped (and why) printed to stderr..SH "BASIC OVERVIEW"Normally, the search starts in the current directory, considering files inall subdirectories.You can use the.I ~/.searchfile to control ways to automatically exclude files.If you don't have this file, a default one will kick in, which automaticallyadd.nf -skip .o .Z .gif.fi(among others) to exclude those kinds of files (which you probably want toskip when searching for text, as is normal).Files that look to be be binary will also be excluded.Files ending with "#" and "~" will also be excluded unless the.B -x~option is given. You can use.B -showrcto show what kinds of files will normally be skipped.See the section on the startup filefor more info.You can use the.B -alloption to indicate you want to consider all files that would otherwise beskipped by the startup file.Based upon various other flags (see "WHICH FILES TO CONSIDER" below),more files might be removed from consideration. For example.nf -mtime 3.fiwill exclude files that aren't at least three days old (change the 3 to -3to exclude files that are more than three days old), while.nf -skip .*.fiwould exclude any file beginning with a dot (of course, '.' and '..' arespecial and always excluded).If you'd like to see what files are being excluded, and why, you can get thelist via the.B \-whyoption.If a file makes it past all the checks, it is then "considered".This usually means it is greped for the regular expressions you gaveon the command line.If any of the regexes match a line, the line is printed.However, if.B -listis given, just the filename is printed. Or, if.B -niceis given, a somewhat more (human-)readable output is generated.If you're searching a huge tree and want to keep informed about howthe search is progressing,.B -vwill print (to stderr) the current directory being searched.Using.B -vvwill also print the current file "every so often", which could be usefulif a directory is huge. Using.B -vvvwill print the update with every file.Below is the full listing of options..SH "OPTIONS TELLING *WHERE* TO SEARCH".TP.BI -dir " DIR"Start searching at the named directory instead of the current directory.If multiple.B -dirarguments are given, multiple trees will be searched..TP.BI -ddir " DIR"Like.B -direxcept it flushes any previous.B -dirdirectories (i.e. "-dir A -dir B -dir C" will search A, B, and C, while"-dir A -ddir B -dir C" will search only B and C. This might be of usein the startup file (see that section below)..TP.B -xdevStay on the same filesystem as the starting directory/directories..TP.B -sortSort the items in a directory before processing them.Normally they are processed in whatever order they happen to be read fromthe directory..TP.B -nolinksDon't follow symbolic links. Normally they're followed..SH "OPTIONS CONTROLLING WHICH FILES TO CONSIDER AND EXCLUDE".TP.BI -mtime " NUM"Only consider files that were last changed more than.I NUMdays ago(less than.I NUMdays if.I NUMhas '-' prepended, i.e. "-mtime -2.5" means to consider files thathave been changed in the last two and a half days)..TP.B -older FILEOnly consider files that have not changed since.I FILEwas last changed.If there is any upper case in the "-older", "or equal" is added to the senseof the test. Therefore, "search -older ./file regex" will never consider"./file", while "search -Older ./file regex" will.If a file is a symbolic link, the time used is that of the file and not thelink..TP.BI -newer " FILE"Opposite of.BR -older ..TP.BI -name " GLOB"Only consider files that match the shell filename pattern.IR GLOB .The check is only done on a file's name (use.B -pathto check the whole path, and use.B -dnameto check directory names).Multiple specifications can be given by separating them with spaces, a'la.nf -name '*.c *.h'.fito consider C source and header files.If.I GLOBdoesn't contain any special pattern characters, a '*' is prepended.This last example could have been given as.nf -name '.c .h'.fiIt could also be given as.nf -name .c -name .h.fior.nf -name '*.c' -name '*.h'.fior.nf -name '*.[ch]'.fi(among others)but in this last case, you have to be sure to supply the leading '*'..TP.BI -path " GLOB"Like.B -nameexcept the entire path is checked against the pattern..TP.B -regex " REGEX"Considers files whose names (not paths) match the given perl regexexactly..TP.BI -iname " GLOB"Case-insensitive version of.BR -name ..TP.BI -ipath " GLOB"Case-insensitive version of.BR -path ..TP.BI -iregex " REGEX"Case-insensitive version of.BR -regex ..TP.BI -dpath " GLOB"Only search down directories whose path matches the given pattern (thisdoesn't apply to the initial directory given by.BI -dir ,of course).Something like.nf -dir /usr/man -dpath /usr/man/man*.fiwould completely skip"/usr/man/cat1", "/usr/man/cat2", etc..TP.BI -dskip " GLOB"Skips directories whose name (not path) matches the given pattern.Something like.nf -dir /usr/man -dskip cat*.fiwould completely skip any directory in the tree whose name begins with "cat"(including "/usr/man/cat1", "/usr/man/cat2", etc.)..TP.BI -dregex " REGEX"Like.BI -dpath ,but the pattern is a full perl regex. Note that this quite differentfrom.B -regexwhich considers only file names (not paths). This option considersfull directory paths (not just names). It's much more useful this way.Sorry if it's confusing.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -