shell學習—關於grep的簡單使用(轉)

BSDLite發表於2007-08-15
shell學習—關於grep的簡單使用(轉)[@more@]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可以檢視得到

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-960238/,如需轉載,請註明出處,否則將追究法律責任。

相關文章