Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2132323
  • 博文数量: 288
  • 博客积分: 10594
  • 博客等级: 上将
  • 技术积分: 3469
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-27 19:27
文章分类

全部博文(288)

文章存档

2012年(4)

2011年(30)

2010年(40)

2009年(32)

2008年(71)

2007年(79)

2006年(32)

分类: LINUX

2007-05-23 22:45:55



grep是在shell中比较常用的工具,常用的选项有:
-c只输出匹配行的计数,例如:

[liuqi@ljjk liuqi]$ grep -c "get" /usr/include/ncurses.h
57

-i不区分大小写(只适用于单字符),例如:
[liuqi@ljjk liuqi]$ grep -i "GET" /usr/include/ncurses.h
#include               /* ...to get mbstate_t, etc. */
#ifdef NCURSES_WGETCH_EVENTS
extern NCURSES_EXPORT(int) wgetch_events(WINDOW *, _nc_eventlist *);    /* experimental */
extern NCURSES_EXPORT(int) wgetnstr_events(WINDOW *,char *,int,_nc_eventlist *);/* experimental */
#endif /* NCURSES_WGETCH_EVENTS */
extern NCURSES_EXPORT(int) attr_get (attr_t *, short *, void *);        /* generated */
extern NCURSES_EXPORT(chtype) getbkgd (WINDOW *);                       /* generated */
extern NCURSES_EXPORT(int) getch (void);                                /* generated */
extern NCURSES_EXPORT(int) getnstr (char *, int);                       /* generated */
extern NCURSES_EXPORT(int) getstr (char *);                             /* generated */
extern NCURSES_EXPORT(WINDOW *) getwin (FILE *);                        /* implemented */
extern NCURSES_EXPORT(int) mvgetch (int, int);                          /* generated */
extern NCURSES_EXPORT(int) mvgetnstr (int, int, char *, int);           /* generated */
extern NCURSES_EXPORT(int) mvgetstr (int, int, char *);                 /* generated */
extern NCURSES_EXPORT(int) mvwgetch (WINDOW *, int, int);               /* generated */
extern NCURSES_EXPORT(int) mvwgetnstr (WINDOW *, int, int, char *, int);       /* generated */
extern NCURSES_EXPORT(int) mvwgetstr (WINDOW *, int, int, char *);      /* generated */
extern NCURSES_EXPORT(int) tigetflag (NCURSES_CONST char *);            /* implemented */
extern NCURSES_EXPORT(int) tigetnum (NCURSES_CONST char *);             /* implemented */
extern NCURSES_EXPORT(char *) tigetstr (NCURSES_CONST char *);          /* implemented */
extern NCURSES_EXPORT(int) ungetch (int);                               /* implemented */
extern NCURSES_EXPORT(int) wattr_get (WINDOW *, attr_t *, short *, void *);    /* generated */
extern NCURSES_EXPORT(int) wgetch (WINDOW *);                           /* implemented */
extern NCURSES_EXPORT(int) wgetnstr (WINDOW *,char *,int);              /* implemented */
extern NCURSES_EXPORT(int) wgetstr (WINDOW *, char *);                  /* generated */
#define wgetstr(w, s)           wgetnstr(w, s, -1)
#define getnstr(s, n)           wgetnstr(stdscr, s, n)
#define gettmode()
#define getyx(win,y,x)          (y = (win)?(win)->_cury:ERR, x = (win)?(win)->_curx:ERR)
#define getbegyx(win,y,x)       (y = (win)?(win)->_begy:ERR, x = (win)?(win)->_begx:ERR)
#define getmaxyx(win,y,x)       (y = (win)?((win)->_maxy + 1):ERR, x = (win)?((win)->_maxx + 1):ERR)
#define getparyx(win,y,x)       (y = (win)?(win)->_pary:ERR, x = (win)?(win)->_parx:ERR)
#define getsyx(y,x) do { if(newscr->_leaveok) (y)=(x)=-1; \
                         else getyx(newscr,(y),(x)); \
#define getattrs(win)           ((win)?(win)->_attrs:A_NORMAL)
#define getcurx(win)            ((win)?(win)->_curx:ERR)
#define getcury(win)            ((win)?(win)->_cury:ERR)
#define getbegx(win)            ((win)?(win)->_begx:ERR)
#define getbegy(win)            ((win)?(win)->_begy:ERR)
#define getmaxx(win)            ((win)?((win)->_maxx + 1):ERR)
#define getmaxy(win)            ((win)?((win)->_maxy + 1):ERR)
#define getparx(win)            ((win)?(win)->_parx:ERR)
#define getpary(win)            ((win)?(win)->_pary:ERR)
#define touchwin(win)           wtouchln((win), 0, getmaxy(win), 1)
#define untouchwin(win)         wtouchln((win), 0, getmaxy(win), 0)
#define attr_get(ap,cp,o)       wattr_get(stdscr,ap,cp,o)
#define getch()                 wgetch(stdscr)
#define getstr(str)             wgetstr(stdscr,str)
#define mvwgetch(win,y,x)               (wmove(win,y,x) == ERR ? ERR : wgetch(win))
#define mvwgetnstr(win,y,x,str,n)       (wmove(win,y,x) == ERR ? ERR : wgetnstr(win,str,n))
#define mvwgetstr(win,y,x,str)          (wmove(win,y,x) == ERR ? ERR : wgetstr(win,str))
#define mvgetch(y,x)                    mvwgetch(stdscr,y,x)
#define mvgetnstr(y,x,str,n)            mvwgetnstr(stdscr,y,x,str,n)
#define mvgetstr(y,x,str)               mvwgetstr(stdscr,y,x,str)
#define getbkgd(win)                    ((win)->_bkgd)
#define wattr_get(win,a,p,opts)         ((void)((a) != 0 && (*(a) = (win)->_attrs)), \
* Pseudo-character tokens outside ASCII range.  The curses wgetch() function
extern NCURSES_EXPORT(int) getmouse (MEVENT *);
extern NCURSES_EXPORT(int) ungetmouse (MEVENT *);
[liuqi@ljjk liuqi]$

作一个对比:

[liuqi@ljjk liuqi]$ grep -c -i "GET" /usr/include/ncurses.h
59
[liuqi@ljjk liuqi]$ grep -c  "GET" /usr/include/ncurses.h
2
[liuqi@ljjk liuqi]$

-h查询多个文件时不显示文件名,例如:

[liuqi@ljjk liuqi]$ grep -c "get" /usr/include/*es.h
/usr/include/aliases.h:4
/usr/include/curses.h:57
/usr/include/features.h:3
/usr/include/gdk_imlib_types.h:0
/usr/include/Imlib_types.h:0
/usr/include/inttypes.h:0
/usr/include/lber_types.h:0
/usr/include/ldap_features.h:1
/usr/include/libmng_types.h:10
/usr/include/ncurses.h:57
/usr/include/nl_types.h:2
/usr/include/values.h:0
[liuqi@ljjk liuqi]$ grep -h -c "get" /usr/include/*es.h
4
57
3
0
0
0
0
1
10
57
2
0
[liuqi@ljjk liuqi]$


查询多文件时只输出包含匹配字符的文件名,例如:

[liuqi@ljjk liuqi]$ grep -h -c "get" /usr/include/*es.h
4
57
3
0
0
0
0
1
10
57
2
0
[liuqi@ljjk liuqi]$ grep -c "get" /usr/include/*es.h
/usr/include/aliases.h:4
/usr/include/curses.h:57
/usr/include/features.h:3
/usr/include/gdk_imlib_types.h:0
/usr/include/Imlib_types.h:0
/usr/include/inttypes.h:0
/usr/include/lber_types.h:0
/usr/include/ldap_features.h:1
/usr/include/libmng_types.h:10
/usr/include/ncurses.h:57
/usr/include/nl_types.h:2
/usr/include/values.h:0
[liuqi@ljjk liuqi]$ grep -h -c -l "get" /usr/include/*es.h
/usr/include/aliases.h
/usr/include/curses.h
/usr/include/features.h
/usr/include/ldap_features.h
/usr/include/libmng_types.h
/usr/include/ncurses.h
/usr/include/nl_types.h
[liuqi@ljjk liuqi]$

-n只显示匹配行及行号,例如:

[liuqi@ljjk liuqi]$ grep -n "get" /usr/include/nl_types.h
27:/* Value for FLAG parameter of `catgets' to say we want XPG4 compliance.  */
44:extern char *catgets (nl_catd __catalog, int __set, int __number,
[liuqi@ljjk liuqi]$

-v显示不包含匹配文本的所由行,例如:

[liuqi@ljjk liuqi]$ grep -v "get" /usr/include/nl_types.h
/* Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#ifndef _NL_TYPES_H
#define _NL_TYPES_H 1

#include

/* The default message set used by the gencat program.  */
#define NL_SETD 1

#define NL_CAT_LOCALE 1


__BEGIN_DECLS

/* Message catalog descriptor type.  */
typedef void *nl_catd;

/* Type used by `nl_langinfo'.  */
typedef int nl_item;

/* Open message catalog for later use, returning descriptor.  */
extern nl_catd catopen (__const char *__cat_name, int __flag) __THROW;

/* Return translation with NUMBER in SET of CATALOG; if not found
   return STRING.  */
                      __const char *__string) __THROW;

/* Close message CATALOG.  */
extern int catclose (nl_catd __catalog) __THROW;

__END_DECLS

#endif /* nl_types.h  */
[liuqi@ljjk liuqi]$


当然,也可以结合正则表达式使用grep来查找文件.
其他详悉内容和使用方法用man可以查看得到
阅读(2426) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~