grep来自英文词组“global search regular expression and print out the line”的缩写,意思是用于全面搜索的正则表达式,并输出相应行。Unix和Linux都直接提供了grep命令。
然而,不管是在CGI界面的MS-DOS系统还是GUI界面的Windows系统中,微软都没有直接提供grep命令,所提供的find命令在功能上与 Unix和Linux上grep命令有较大的差距。
在编写Windows应用程序的过程中,我们经常需要调用Windows API函数,查询结构体或类的定义。对于使用MASM32来编写Windows应用程序的用户来说,由于MASM32集成的.inc文件与Windows SDK的.h并不完全是一一对应的,因此经常需要在masm32的.inc文件中进行搜索,Windows内置的Find命令难以满足我们的要求。
幸好Borland(曾一度改名为Inprise)在他的开发工具中,不管是Delphi,还是C++ Builder,都提供了grep工具——grep.exe,比较常见的是5.5版本。我们可以把这个grep.exe复制到%SystemRoot%\system32(通常是C:\WINDOWS\system32)下,这样我们就随时可以在命令提示符窗口中使用grep了。
在命令提示符窗口中输入 grep ?可以查看grep命令的使用说明:
C:\>grep ?
Turbo GREP 5.5 Copyright (c) 1992, 1999 Inprise Corporation
Syntax: GREP [-rlcnvidzuwo] searchstring file[s] or @filelist
Options are one or more option characters preceded by "-", and optionally
followed by "+" (turn option on), or "-" (turn it off). The default is "+".
-r+ Regular expression search -l- File names only
-c- match Count only -n- Line numbers
-v- Non-matching lines only -i- Ignore case
-d- Search subdirectories -z- Verbose
-e Next argument is searchstring -w- Word search
-o- UNIX output format Default set: [0-9A-Z_]
-q- Quiet: supress normal output
-u xxx Create a copy of grep named 'xxx' with current options set as default
A regular expression is one or more occurrences of: One or more characters
optionally enclosed in quotes. The following symbols are treated specially:
^ start of line $ end of line
. any character \ quote next character
* match zero or more + match one or more
[aeiou0-9] match a, e, i, o, u, and 0 thru 9 ;
[^aeiou0-9] match anything but a, e, i, o, u, and 0 thru 9
C:\>
翻译过来就是:
语法: GREP [-rlcnvidzuwo] 搜索字符串 文件或@filelist
选项是一个或多个以“-”开头的选项字符,并且可以选择后跟“+”(打开选项)或“-”(将其关闭)。 默认值为“+”。
-r+ 正则表达式搜索 -l- 仅文件名
-c- 匹配 仅计数 -n- 行号
-v- 仅不匹配的行 -i- 忽略大小写
-d- 搜索子目录 -z- 详细
-e 下一个参数是搜索字符串 -w- 单词搜索
-o- UNIX 输出格式 默认设置:[0-9A-Z_]
-q- 安静:抑制正常输出
-u xxx 创建一个名为“xxx”的 grep 副本,并将当前选项设置为默认值
正则表达式是以下一项或多次出现: 一个或多个字符可选地用引号括起来。以下符号经过特殊处理:
^ 行首 $ 行尾
. 任何字符 \ 引号下一个字符
* 匹配零个或多个 + 匹配一个或多个
[aeiou0-9] 匹配 A、E、I、O、U 和 0 到 9
[^aeiou0-9] 匹配除 a、e、i、o、u 和 0 到 9 以外的任何内容
其中最常用的就是 -r 支持使用正则表达式搜索、-d搜索子目录、-i忽略大小写。
比如我们要在z盘的\masm32\include下的所有.inc文件中搜索RtlIpv6AddressToString函数的声明,可以使用以下命令:
grep -d+ -i+ "RtlIpv6AddressToString" z:\masm32\include\*.inc
执行结果如下:
C:\>grep -d+ -i+ "RtlIpv6AddressToString" z:\masm32\include\*.inc
File z:\masm32\include\dnslib.inc:
RtlIpv6AddressToStringA PROTO STDCALL :DWORD,:DWORD
RtlIpv6AddressToString equ <RtlIpv6AddressToStringA>
RtlIpv6AddressToStringW PROTO STDCALL :DWORD,:DWORD
RtlIpv6AddressToString equ <RtlIpv6AddressToStringW>
File z:\masm32\include\ntdll.inc:
RtlIpv6AddressToStringA PROTO STDCALL :DWORD,:DWORD
RtlIpv6AddressToString equ <RtlIpv6AddressToStringA>
RtlIpv6AddressToStringExA PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
RtlIpv6AddressToStringEx equ <RtlIpv6AddressToStringExA>
RtlIpv6AddressToStringExW PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
RtlIpv6AddressToStringEx equ <RtlIpv6AddressToStringExW>
RtlIpv6AddressToStringW PROTO STDCALL :DWORD,:DWORD
RtlIpv6AddressToString equ <RtlIpv6AddressToStringW>
File z:\masm32\include\ntoskrnl.inc:
RtlIpv6AddressToStringA PROTO STDCALL :DWORD,:DWORD
RtlIpv6AddressToString equ <RtlIpv6AddressToStringA>
RtlIpv6AddressToStringExA PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
RtlIpv6AddressToStringEx equ <RtlIpv6AddressToStringExA>
RtlIpv6AddressToStringExW PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
RtlIpv6AddressToStringEx equ <RtlIpv6AddressToStringExW>
RtlIpv6AddressToStringW PROTO STDCALL :DWORD,:DWORD
RtlIpv6AddressToString equ <RtlIpv6AddressToStringW>
File z:\masm32\include\wdmsec.inc:
RtlIpv6AddressToStringA PROTO STDCALL :DWORD,:DWORD
RtlIpv6AddressToString equ <RtlIpv6AddressToStringA>
RtlIpv6AddressToStringExA PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
RtlIpv6AddressToStringEx equ <RtlIpv6AddressToStringExA>
RtlIpv6AddressToStringExW PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
RtlIpv6AddressToStringEx equ <RtlIpv6AddressToStringExW>
RtlIpv6AddressToStringW PROTO STDCALL :DWORD,:DWORD
RtlIpv6AddressToString equ <RtlIpv6AddressToStringW>
C:\>
可以看到,
z:\masm32\include\dnslib.inc
z:\masm32\include\ntdll.inc
z:\masm32\include\ntoskrnl.inc
z:\masm32\include\wdmsec.inc
均包括了RtlIpv6AddressToString函数的声明。