首页
社区
课程
招聘
[原创]编程工具之Grep 使用介绍
发表于: 2008-4-5 07:22 8717

[原创]编程工具之Grep 使用介绍

2008-4-5 07:22
8717

###########################################################
编程工具之 Grep 使用介绍
大家使用过Borland 编程语言系列产品嘛。
现在来介绍使用这个Grep (General Regular Expression Parse)工具。

它在Borland 编程语言产品中都能见到它的踪迹。
Grep 是个文字比对程序,在Windows GUI 尚未流行之前它就存在了。
如同Unix 所提供的grep 文字比对程式。

早在Dos 3.1 时代吧,那时我的使用方法最大的用处,
就是在众多吓死人include *.h 内容中搜寻某个函数字串。
当然也可以用来搜寻一般文数字字串的。

使用方法::
01.
直接在命列行下打入grep.exe ?
就会列出相关使用参数

[D:\OO\Bcb6\Bin]D:\OO\Bcb6\Bin\grep.exe ?
Turbo GREP 5.5 Copyright (c) 1992, 2000 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

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 7
支持
分享
最新回复 (2)
雪    币: 234
活跃值: (10)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
2
刚刚找到一个 GNU grep version 2.0d 版。
这里可以下载使用 GREP20d_WIN.ZIP
(它包含能直接使用的grep.exe 以及C source code
喜欢编程者可以演究source code)。

这是for Windows 的版本,打入 grep.exe -?
一样也就列出使用参数。

看起来比我之前介绍的Grep版本参数还要多。
算是增强版不错用,
呵呵!

D:\OO\_Grep\grep20dw\grep.exe
[D:\OO\_Grep\grep20dw]D:\OO\_Grep\grep20dw\grep.exe -?
D:\OO\_Grep\grep20dw\grep.exe: option requires an argument -- ?
GNU grep version 2.0d

SYNOPOSIS
grep [-[[AB] ]<num>] [-[CEFGLSVbchilnqsvwx?]] [-[ef]] <expr> [<files...>]

DESCRIPTION

Grep searches the named input files (or standard input if no files are named, or
the file name - is given) for lines containing a match to the given pattern.
By default, grep prints the matching lines.  There are three major variants of

grep, controlled by the following options.
-G Interpret pattern as a basic regular expression (see below).  This is
   the default.
-E Interpret pattern as an extended regular expression (see below).
-F Interpret pattern as a list of fixed strings, separated by newlines,
   any of which is to be matched.

In addition, two variant programs egrep and fgrep are available.  Egrep is
similiar (but not identical) to "grep -E", and is compatible with the
historical Unix egrep.  Fgrep is the same as "grep -F".

All variants of grep understand the following options:

-num Matches will be printed with num lines of leading and trailing
    context.  However, grep will never print any given line more than once.
-A  "num" Print num lines of trailing context after matching lines.
-B  "num" Print num lines of leading context before matching lines.
-C  Equivalent to -2.
-S  Search subdirectories.
-V  Print the version number of grep to standard error.  This version
    number should be included in all bug reports (see below).
-b  Print the byte offset within the input file before each line of output.
-c  Suppress normal output; instead print a count of matching lines for
    each input file.  With the -v option (see below), count non-matching
    lines.
-e  "pattern" Use pattern as the pattern; useful to protect patterns
    beginning with -.
-f  "file" Obtain the pattern from file.
-h  Suppress the prefixing of filenames on output when multiple files are
    searched.
-i  Ignore case distinctions in both the pattern and the input files.
-L  Suppress normal output; instead print the name of each input file from
    which no output would normally have been printed.
-l  Suppress normal output; instead print the name of each input file from
    which output would normally have been printed.
-n  Prefix each line of output with the line number within its input file.
-q  Quiet; suppress normal output.
-s  Suppress error messages about nonexistent or unreadable files.
-v  Invert the sense of matching, to select non-matching lines.
-w  Select only those lines containing matches that form whole words.  The
    test is that the matching substring must either be at the beginning of
    the line, or preceded by a non-word constituent character.  Similarly,
    it must be either at the end of the line or followed by a non-word
    constituent character.  Word-constituent characters are letters, digits,
    and the underscore.
-x  Select only those matches that exactly match the whole line.
-?  Displays this help.

Win32 port with subdirectory search created by Tim Charron
(full source available at http://www.interlog.com/~tcharron/grep.html)
2008-4-5 07:22
0
雪    币: 234
活跃值: (10)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
3
譬如说我有个Borland C++ Builder6 Include 路径中有这么多东西
D:\OO\Bcb6\Include\*.h
想找个凡是头档中有函数"CreateWindow" 文字
此时就使用这方式
D:\OO\_Grep\grep20dw\grep.exe -A 1 -n -S "CreateWindow" D:\OO\Bcb6\Include\*.h

D:\OO\Bcb6\Include\
 <DIR>      2007/07/02  0:40   +--Mfc
 <DIR>      2007/07/02  0:40   |  +--Chs
 <DIR>      2007/07/02  0:40   |  +--Cht
 <DIR>      2007/07/02  0:40   |  +--Deu
 <DIR>      2007/07/02  0:40   |  +--Esp
 <DIR>      2007/07/02  0:40   |  +--Fra
 <DIR>      2007/07/02  0:40   |  +--Ita
 <DIR>      2007/07/02  0:40   |  +--Jpn
 <DIR>      2007/07/02  0:40   |  +--Kor
 <DIR>      2007/07/02  0:40   |  \--res
 <DIR>      2007/07/02  0:41   +--Atl
 <DIR>      2007/07/02  0:41   +--Gl
 <DIR>      2007/07/02  0:41   +--Idl
 <DIR>      2007/07/02  0:41   +--Rw
 <DIR>      2007/07/02  0:41   +--Sys
 <DIR>      2007/07/02  0:41   +--Vcl
 <DIR>      2007/07/02  0:41   +--Config
 <DIR>      2007/07/02  0:41   +--oldstl
 <DIR>      2007/07/02  0:41   +--stl
 <DIR>      2007/07/02  0:41   |  \--debug
 <DIR>      2007/07/02  0:41   +--Stlport
 <DIR>      2007/07/02  0:41   +--Using
      1393  2004/06/17 22:30   |  +  cstring
       107  2004/06/17 22:30   |  +  export
       637  2004/06/17 22:30   |  +  fstream
       252  2004/06/17 22:30   |  +  iomanip
      1321  2004/06/17 22:30   |  +  ios
      2128  2004/06/17 22:30   |  +  iosfwd
       332  2004/06/17 22:30   |  +  iostream
       442  2004/06/17 22:30   |  +  istream
      2375  2004/06/17 22:30   |  +  locale
       284  2004/06/17 22:30   |  +  ostream
       608  2004/06/17 22:30   |  +  sstream
       173  2004/06/17 22:30   |  +  streambuf
       173  2004/06/17 22:30   |  \  strstream
       624  2004/06/17 22:30   +  HASH_MAP.H
       624  2004/06/17 22:30   \  HASH_SET.H



[D:\OO\_Grep\grep20dw]D:\OO\_Grep\grep20dw\grep.exe -A 1 -n -S "CreateWindow" D:\OO\Bcb6\Include\
*.h
D:\OO\Bcb6\Include\commctrl.h:6033:            CreateWindow(ANIMATE_CLASS, NULL,           \
D:\OO\Bcb6\Include\commctrl.h-6034-                dwStyle, 0, 0, 0, 0, hwndP, (HMENU)(id), hInst
ance, NULL)
--
D:\OO\Bcb6\Include\dispdib.h:208:    return CreateWindow(DISPLAYDIB_WINDOW_CLASS,"",dwStyle,0, 0,

D:\OO\Bcb6\Include\dispdib.h-209-            GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CY
SCREEN),
--
D:\OO\Bcb6\Include\mapinls.h:118:#define CreateWindowA                                  CreateWin
dow
D:\OO\Bcb6\Include\mapinls.h-119-#define RegisterClassA                                 RegisterC
lass
--
D:\OO\Bcb6\Include\winuser.h:1284:CreateWindowStationA(
D:\OO\Bcb6\Include\winuser.h-1285-    IN LPCSTR              lpwinsta,
--
D:\OO\Bcb6\Include\winuser.h:1292:CreateWindowStationW(
D:\OO\Bcb6\Include\winuser.h-1293-    IN LPCWSTR              lpwinsta,
--
D:\OO\Bcb6\Include\winuser.h:1298:#define CreateWindowStation  CreateWindowStationW
D:\OO\Bcb6\Include\winuser.h-1299-#else
D:\OO\Bcb6\Include\winuser.h:1300:#define CreateWindowStation  CreateWindowStationA
D:\OO\Bcb6\Include\winuser.h-1301-#endif // !UNICODE
--
D:\OO\Bcb6\Include\winuser.h:3568: * Special value for CreateWindow, et al.
D:\OO\Bcb6\Include\winuser.h-3569- */
--
D:\OO\Bcb6\Include\winuser.h:3579:CreateWindowExA(
D:\OO\Bcb6\Include\winuser.h-3580-    IN DWORD dwExStyle,
--
D:\OO\Bcb6\Include\winuser.h:3595:CreateWindowExW(
D:\OO\Bcb6\Include\winuser.h-3596-    IN DWORD dwExStyle,
--
D:\OO\Bcb6\Include\winuser.h:3609:#define CreateWindowEx  CreateWindowExW
D:\OO\Bcb6\Include\winuser.h-3610-#else
D:\OO\Bcb6\Include\winuser.h:3611:#define CreateWindowEx  CreateWindowExA
D:\OO\Bcb6\Include\winuser.h-3612-#endif // !UNICODE
--
D:\OO\Bcb6\Include\winuser.h:3614:#define CreateWindowA(lpClassName, lpWindowName, dwStyle, x, y,
\
D:\OO\Bcb6\Include\winuser.h-3615-nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)\
D:\OO\Bcb6\Include\winuser.h:3616:CreateWindowExA(0L, lpClassName, lpWindowName, dwStyle, x, y,\
D:\OO\Bcb6\Include\winuser.h-3617-nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)
D:\OO\Bcb6\Include\winuser.h:3618:#define CreateWindowW(lpClassName, lpWindowName, dwStyle, x, y,
\
D:\OO\Bcb6\Include\winuser.h-3619-nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)\
D:\OO\Bcb6\Include\winuser.h:3620:CreateWindowExW(0L, lpClassName, lpWindowName, dwStyle, x, y,\
D:\OO\Bcb6\Include\winuser.h-3621-nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)
--
D:\OO\Bcb6\Include\winuser.h:3623:#define CreateWindow  CreateWindowW
D:\OO\Bcb6\Include\winuser.h-3624-#else
D:\OO\Bcb6\Include\winuser.h:3625:#define CreateWindow  CreateWindowA
D:\OO\Bcb6\Include\winuser.h-3626-#endif // !UNICODE
--\OO\Bcb6\Include\Mfc
D:\OO\Bcb6\Include\Mfc\afxctl.h:835:    void CreateWindowForSubclassedControl();
D:\OO\Bcb6\Include\Mfc\afxctl.h-836-    BOOL IgnoreWindowMessage(UINT msg, WPARAM wParam, LPARAM
lParam,
--
D:\OO\Bcb6\Include\Mfc\afxcview.h:76:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxcview.h-77-
--
D:\OO\Bcb6\Include\Mfc\afxcview.h:103:  virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxcview.h-104-
--
D:\OO\Bcb6\Include\Mfc\afxext.h:205:    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxext.h-206-    virtual void PostNcDestroy();
--
D:\OO\Bcb6\Include\Mfc\afxext.h:294:    BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxext.h-295-    BOOL AllocElements(int nElements, int cbElement);
--
D:\OO\Bcb6\Include\Mfc\afxext.h:848:    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxext.h-849-    // printing support
--
D:\OO\Bcb6\Include\Mfc\afxrich.h:199:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxrich.h-200-
--
D:\OO\Bcb6\Include\Mfc\afxwin.h:1981:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxwin.h-1982-
--
D:\OO\Bcb6\Include\Mfc\afxwin.h:3258:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxwin.h-3259-   virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
--
D:\OO\Bcb6\Include\Mfc\afxwin.h:3363:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxwin.h-3364-   virtual BOOL LoadFrame(UINT nIDResource,
--
D:\OO\Bcb6\Include\Mfc\afxwin.h:3429:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxwin.h-3430-   virtual BOOL LoadFrame(UINT nIDResource, DWORD dwDefaultS
tyle,
--
D:\OO\Bcb6\Include\Mfc\afxwin.h:3519:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxwin.h-3520-
--
D:\OO\Bcb6\Include\Mfc\afxwin.h:3633:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxwin.h-3634-   virtual void PostNcDestroy();
--
D:\OO\Bcb6\Include\Mfc\afxwin.h:3686:   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
D:\OO\Bcb6\Include\Mfc\afxwin.h-3687-
--\OO\Bcb6\Include\Atl
D:\OO\Bcb6\Include\Atl\atlwin.h:179:            m_hWnd = ::CreateWindowEx(dwExStyle, lpstrWndClas
s, szWindowName,
D:\OO\Bcb6\Include\Atl\atlwin.h-180-                    dwStyle, rcPos.left, rcPos.top, rcPos.rig
ht - rcPos.left,
--
D:\OO\Bcb6\Include\Atl\atlwin.h:192:            m_hWnd = ::CreateWindowEx(dwExStyle, lpstrWndClas
s, szWindowName,
D:\OO\Bcb6\Include\Atl\atlwin.h-193-                    dwStyle, lpRect->left, lpRect->top, lpRec
t->right - lpRect->left,
--
D:\OO\Bcb6\Include\Atl\atlwin.h:2183:   HWND hWnd = ::CreateWindowEx(dwExStyle, (LPCTSTR)MAKELONG
(atom, 0), szWindowName,
D:\OO\Bcb6\Include\Atl\atlwin.h-2184-           dwStyle, rcPos.left, rcPos.top, rcPos.right - rcP
os.left,
--
D:\OO\Bcb6\Include\Atl\atlwin.h:2697:           HWND hWnd = ::CreateWindowEx(dwExStyle, (LPCTSTR)
MAKELONG(atom, 0), szWindowName,
D:\OO\Bcb6\Include\Atl\atlwin.h-2698-                                                           d
wStyle,

2008-4-5 07:51
0
游客
登录 | 注册 方可回帖
返回
//