find

最后更新:2012-01-19, Ver 2.3.3.0119

 

简介

 - 在文件中查找匹配的字符串
  • 支持一次性指定多个文件通配符和文件列表。
  • 支持管道模式,与其它命令协同工作。
  • 支持包含子目录,与 grep 不同,即使查找目录中不包含任何匹配文件,也会继续子目录查找。
  • 支持普通匹配、正则匹配、可忽略大小写、可跨行匹配。
  • 支持 TCL 8.2 兼容的高级正则表达式(ARE)。
  • 同时支持 posix 标准的扩展正则表达式及 perl 风格的正则匹配。
  • 支持 DOS(Windows)、Macintosh 和 unix 风格的换行符,可选择自动识别(默认)或手动指定。
  • 可完全自定义的匹配结果输出格式,更方便查看或与其它软件配合。
  • 统计功能,列出每个文件中的匹配次数、总匹配次数等,也可以仅列出发现匹配的文件列表。
  • 支持 Win32 和纯 DOS 环境(纯DOS环境中需要 HX DOS Extender 支持)。
  • 支持 POSIX 环境,提供 linux x86/x64、FreeBSD、NetBSD、Solaris 等版本下载。

 

更新历史

2012-01-19, Ver 2.3.3.0119

NEW: 新增 -co 选项: 对同一行中的多次匹配结果,仅输出一次。

2010-02-16, Ver 2.3.2.0216

WRK: [Windows Only] 解决了 Windows 的输出重定向操作会将内容中的 "\n" 全部替换为 "\r\n" 的问题(意即:Windows 的输出重定向操作会将内容中的 "\r\n" 全部替换为 "\r\r\n")。

2009-12-13, Ver 2.3.1.1213

NEW: 新增 -errstop 参数。使用此参数时,遇到无法访问的文件和子目录会报错并自动终止查找。不使用此参数时,find 会自动跳过无法访问的子目录。对于无法打开的文件,find 会输出一行报错信息,但不会终止搜索。

2009-03-12, Ver 2.3.0.0312

FIX: 修正了在使用 /r 或 /ric 参数并且使用 ARE 'p' 和 's' 模式进行匹配时,'^' 有时仍然会匹配行首的问题。
     例如:在 dos 格式的文件中,/r:"***:(?p)(^.*)\n" 会匹配所有行,而不仅仅是文件的第一行。
NEW: 为格式化输出('-o:')新增 'k' 字段。k 字段精确地输出所有匹配的块,包括多行匹配的完整内容。
NEW: 新增行匹配('-l:')选项,此选项匹配文件中指定范围的行,比起使用复杂的 ARE 表达式来完成同样的功能,行匹配选项更简单灵活。

2008-09-21, Ver 2.2.1.0921

NEW: 新增 unicode 选项和 'b'/'B' 输出格式。

2008-09-09, Ver 2.1.4.0909

UPD: 增强了正则行首锚点 '^' 的适应性。

2008-09-08, Ver 2.1.3.0908

FIX: 纠正了正则表达式零长匹配时会出现无限循环的问题(例如,在内容“aaa.bbb”中匹配“[^.]*”)。
FIX: 纠正了正则行首锚点解析不正确的问题(例如,在内容“ccc”中匹配“^c”时,将为每个‘c’产生一次匹配)

2008-08-16, Ver 2.1.2.715

新增高级正则表达式(ARE)支持

2006-07-28, Ver 2.0.5.728

发布

 

典型应用

  1. 在命令行中单独使用,或作为管道过滤器使用。例如:“expand -d *.cab | f -ric:g.*.(dll|exe)”在当前目录下的所有cab文件中查找g开头的dll或exe文件。
     
  2. 在批处理中应用
     
  3. 与其它工具配合使用,例如:vim中键入:“:set grepprg=f\ /o:F:l:o”后,可以为vim加入批量匹配的功能。这时使用vim命令:“:grep *.h *.c *.cpp *.hpp *.cxx /s /r:test.*string”搜索当前目录及子目录下所有源文件。之后可以使用 :cn 命令跳转到下一个匹配;:cnf 命令跳转到下一个文件中的第一个匹配等等。

 

简单中文帮助

C:\Documents and Settings\Administrator>
#f

===============================================================================
          find Ver 2.3.3.0119 by BaiYang / 2004 - 2012, 免费软件
                            http://baiy.cn
===============================================================================
在文件中匹配字符串

用法: f [文件通配符1 文件通配符2 ...] <查找选项>:<匹配串> [其它选项]

===============================================================================
查找选项:
  -f:        - 匹配精确串
  -fic:      - 匹配精确串(忽略大小写)
  -r:        - 正则表达式匹配
  -ric:      - 正则表达式匹配(忽略大小写)
  -rnnl:     - 跨行正则匹配
  -rnnlic:   - 跨行正则匹配(忽略大小写)
  -l:nnn-mmm - 匹配从 'nnn' 行开始,直到 'mmm' 行的所有内容。如果 '-mmm' 被忽略
               则从 'nnn' 行开始一直匹配到文件结束

===============================================================================
其它选项:
  -s       - 包含子目录
  -stdin   - 从标准输入设备获得要查找的内容(如果未指定任何文件模板的话,则自动
             启用该选项
  -o:      - 指定输出格式,以下关键字:
               f  文件名(相对路径)
               F  文件名(绝对路径)
               o  匹配起始处在行中的字符偏移
               x  匹配起始处在行中的字符偏移(十六进制)
               b  匹配起始处在行中的字节偏移
               s  匹配串的字符长度
               S  匹配串的字节长度
               O  匹配起始处在整个文件中的字符偏移
               X  匹配起始处在整个文件中的字符偏移(十六进制)
               B  匹配起始处在整个文件中的字节偏移
               l  行号
               c  行的内容
               C  整个文件的内容
               k  精确地输出匹配的文本块,包含多行匹配的所有内容
               t  制表符(Tab)
               r  回车符
               n  换行符
             默认格式: 普通模式时:'f:l:c'(文件相对路径:行:行内容);
                       使用 -stdin 选项时:'c';使用 -l: 匹配时:'k'
  -of:     - 仅列出匹配的文件,该选项使 “-o:” 选项无效,关键字为:
               f  文件名(相对路径)
               F  文件名(绝对路径)
             默认值: 如果使用 "-of" 时,未指定任何关键字,默认列出相对路径
  -co      - 使用紧凑输出模式:对同一行中的多次匹配结果,仅输出一次。
  -fmc     - 列出每个文件的匹配计数
  -tmc     - 列出总匹配计数
  -format: - 指定文件换行符格式,可以是:"auto", "dos", "unix" 或 "mac"
             默认: auto(自动)
  -unicode - 使 find 将输入文件按 unicode (utf-16) 格式进行解析
             默认: 按 ANSI/MBCS 编码进行解析
  -errstop - 当遇到文件或子目录访问错误时,终止搜索任务。
             默认: 遇到无法访问的子目录访跳过;遇到无法访问的文件时,打印错误
             信息并继续

===============================================================================
POSIX 和 Perl 风格的正则表达式:

  - "find" 完整支持 POSIX.2 扩展标准及 Perl 风格的正则表达式。
    以下是支持的字符类及其对应关系:
       POSIX           perl       描述
       --------------------------------------------------------------------
       [:alnum:]                  字母和数字
       [:alpha:]       \a         字母
       [:lower:]       \l         小写字母
       [:upper:]       \u         大写字母
       [:blank:]                  空格和制表符
       [:space:]       \s         空白字符
       [:cntrl:]                  控制字符
       [:digit:]       \d         十进制数字
       [:xdigit:]      \x         十六进制数字
       [:graph:]                  可打印字符(不包括空白符)
       [:print:]       \p         可打印字符(包括空白符)
       [:punct:]                  标点符号

  - 以下是一些 perl 的特殊字符类:
       perl   POSIX等效         描述
       --------------------------------------------------------------------
       \o     [0-7]             八进制数字
       \O     [^0-7]            非八进制数字
       \w     [[:alnum:]_]      单词组成
       \W     [^[:alnum:]_]     非单词组成
       \A     [^[:alpha:]]      非字母 
       \L     [^[:lower:]]      非小写字母
       \U     [^[:upper:]]      非大写字母
       \S     [^[:space:]]      非空白符
       \D     [^[:digit:]]      非十进制数字
       \X     [^[:xdigit:]]     非十六进制数字
       \P     [^[:print:]]      非可打印字符
       \<     [^[:alpha:]_]     单词开始
       \>     [^[:alnum:]_]     单词结束

  - 注意: posix 字符类是必须工作在集合中的(“[”和“]”内)。相反,perl 风格
          的字符类是工作在集合运算之外的。

  - 此外,为了便于在命令行输入一些特殊字符,特别定义了一下别名:
       perl风格      POSIX风格       描述
       -----------------------------------------------------------------------
       \"            [:dq:]          双引号
       \'            [:sq:]          单引号
       \t            [:tb:]          制表符
       \n            [:nl:]          换行符 (0x0A)
       \r            [:rt:]          回车符 (0x0D)
       \b            [:bs:]          退格符

===============================================================================
TCL 8.2 兼容的高级正则表达式(ARE)

  - 为 -r:, -ric:, -rnnl:, -rnnlic:  等命令添加 '***:' 前缀即可开启高级正则表达
    式功能。ARE 提供了比 POSIX ERE/BRE 更为强大的正则匹配语法,并且使用一套自定
    义的字符类。有关 ARE 的详细信息,请访问:
    http://baiy.cn/utils/_regex_doc/index.htm

===============================================================================
返回码:
  -1   - 搜索时发生错误
   0   - 搜索成功(发现匹配)
   1   - 搜索失败(未发现匹配)

===============================================================================
开关的前缀和后缀:
  * 所有命令行开关(选项)都是大小写无关的(如: "-fic:" 和 "-FIC:")
  * 开关的前缀可以是 "-" 或 "/"(如: "/s" 和 "-s")
  * 开关的后缀可以是 ":" 或 "="(如: "/f:", "/f=", "-f:" 和 "-f=" 等效)

===============================================================================
应用示例:
  f *.txt *.htm -f:something
  f *.cpp *.hpp *.c *.h -fic:myfunc -o:"[F]:[l]:[o]nc"
  f *.cpp *.h -r:"***:#define.*?MY_DEF"
  type myfile.txt | f -f:BaiYang
  expand -d *.cab | f -f:gdi32.dll

 

帮助屏幕

C:\Documents and Settings\Administrator>
#f

===============================================================================
            find Ver 2.3.3.0119 by BaiYang / 2004 - 2012, Freeware
                                 http://baiy.cn
===============================================================================
Find string in file(s)

USAGE: f [filePattern1 filePattern2 ...] <findOption>:<find> [otherOptions]

===============================================================================
FIND OPTION:
  -f:        - find
  -fic:      - find ignore case
  -r:        - regular expresion find
  -ric:      - regular expresion find ignore case
  -rnnl:     - regular expresion find no newline
  -rnnlic:   - regular expresion find no newline and ignore case
  -l:nnn-mmm - find contents from line 'nnn' to 'mmm', if 'mmm' is omited, all
               contents from line 'nnn' will be matched.

===============================================================================
OTHER OPTION:
  -s       - include sub dirs
  -stdin   - use standard input device (keyboard or pipe) instead of files
             default if no file pattern is given
  -o:      - output format, key word is:
               f  filename - relatively path
               F  filename - absolutely path
               o  line offset
               x  line offset (hex)
               s  matched string's char size
               S  matched string's byte size
               O  file offset
               X  file offset (hex)
               b  line offset by bytes
               B  file offset by bytes
               l  line number
               c  line content
               C  the file content (CAUTION: maybe very large)
               k  whole matched content blocK. (maybe multi lines.)
               t  table
               r  return
               n  new line
             DEFAULT:'f:l:c'with normal mode; 'c' on stdin and 'k' on line mode
  -of:     - only list the matching file(s), this options overwrite the "-o:"
             option. this option's key word can only be:
               f  filename - relatively path
               F  filename - absolutely path
             DEFAULT: f (if "-of" has specified with no value).
  -co      - apply compact output: only show the first match if more then one
             matchs are found in the same line.
  -fmc     - Show File Match Count (match count of per file)
  -tmc     - Show Total Match Count
  -format: - file format, can be "auto" "dos", "unix" or "mac"
             DEFAULT: auto
  -unicode - tell find parsing the input as an unicode (utf-16) file.
             DEFAULT: deal input files as ANSI/MBCS encoding
  -errstop - stop on a file or sub-directory access error (the old behave).
             DEFAULT: ignore the access error and try to continue.

===============================================================================
POSIX and perl style Regular Expression:

  - "find" fully support POSIX.2 Extended and Perl style Regular Expresion.
    Here is a list of they character classes:
       POSIX           perl       Description
       --------------------------------------------------------------------
       [:alnum:]                  letters and digits
       [:alpha:]       \a         letters
       [:lower:]       \l         lowercase letters
       [:upper:]       \u         uppercase letters
       [:blank:]                  space and tab characters
       [:space:]       \s         whitespace characters
       [:cntrl:]                  control characters
       [:digit:]       \d         decimal digits
       [:xdigit:]      \x         hexadecimal digits
       [:graph:]                  printable characters excluding space
       [:print:]       \p         printable characters including space
       [:punct:]                  punctuation characters

  - And here a some special char classes in perl:
       perl   POSIX equivalent  Description
       --------------------------------------------------------------------
       \o     [0-7]             octal digit
       \O     [^0-7]            non-octal digit
       \w     [[:alnum:]_]      word character
       \W     [^[:alnum:]_]     non-word character
       \A     [^[:alpha:]]      non-alphabetic character
       \L     [^[:lower:]]      non-lowercase character
       \U     [^[:upper:]]      non-uppercase character
       \S     [^[:space:]]      non-whitespace character
       \D     [^[:digit:]]      non-digit
       \X     [^[:xdigit:]]     non-hex digit
       \P     [^[:print:]]      non-printable characters
       \<     [^[:alpha:]_]     begin of word
       \>     [^[:alnum:]_]     end of word

  - note: posix char class must working in the square brackets. contrary,
          perl's must stay outside of the brackets.

  - And several alias has been created to help input some special char:
       perl style    POSIX style     Description
       -----------------------------------------------------------------------
       \"            [:dq:]          double quotation
       \'            [:sq:]          single quotation
       \t            [:tb:]          table
       \n            [:nl:]          new line (0x0A)
       \r            [:rt:]          return (0x0D)
       \b            [:bs:]          backspace

===============================================================================
TCL 8.2 Compatibal ARE expression:

  - Use '***:' prefix with -r:, -ric:, -rnnl: or -rnnlic: to enable Advanced
    Regular Expression mode. The ARE mode have more powerful expression then
    POSIX BRE and ERE rule. And it has it's own char classes. More info at:
    http://baiy.cn/utils/_regex_doc/index.htm

===============================================================================
RETURN CODE:
  -1   - error occured when searching
   0   - search successful
   1   - search failed (pattern not found)

===============================================================================
SWITCH PREFIX and SUFFIX:
  * All switchs (options) are case-insensitive (i.e: "-fic:" or "-FIC:")
  * Switch Prefix can be either "-" or "/" (i.e: "/s" or "-s")
  * Switch Suffix can be either ":" or "=" (i.e: "/f:", "/f=", "-f:" or "-f=")

===============================================================================
EXAMPLE:
  f *.txt *.htm -f:something
  f *.txt -l:15-20
  f *.cpp *.hpp *.c *.h -fic:myfunc -o:"[F]:[l]:[o]nc"
  f *.cpp *.h -r:"***:#define.*?MY_DEF"
  type myfile.txt | f -f:BaiYang
  expand -d *.cab | f -f:gdi32.dll

 

POSIX 版本说明

安装步骤:
  1. 下载 f_xxx_yyy.zip (其中 xxx 为操作系统,yyy 为硬件平台)。
  2. 使用 upzip 命令解压:'unzip f_xxx_yyy.zip'。
  3. 复制:'cp f /bin'

注意事项:

  • 不同于 Windows/DOS 平台,由于 '/' 为 posix 路径分割符,所以开关前缀只能使用 '-',例如:'-f:1234' 等。
  • 由于 posix shell 自动展开文件通配符,这将导致匹配子目录功能不正常,所以应当使用单引号或双引号引用文件模板,例如:f '/etc/*' -s -r:"192.168.155.\d+" 等。

 

下载

f.rar            (Windows/DOS,DOS 下使用需要 HXDOS 环境支持)
f_linux_x86.zip  (在 Ubuntu 8.04 上测试通过)
f_linux_x64.zip  (在 Ubuntu 8.04 上测试通过)
f_freebsd_x64.zip(在 FreeBSD 7.0 上测试通过)
f_netbsd_x86.zip (在 NetBSD 5.0.1 上测试通过)
f_solaris_x86.zip(在 OpenSolaris 2009.06 上测试通过)