6. redhat grep、egrep、fgrep

本文详细介绍了Unix下的grep家族,包括grep、egrep和fgrep的基本用法及高级技巧,提供了丰富的实例帮助理解各种复杂的正则表达式匹配。

一、grep家族简介

1、Unix的grep家族包括:grep、egrep和fgrep

1)grep

(1)grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出

(2)grep的工作方式:它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用(双引号或者单引号),模板后的所有字符串被看作文件名。搜索的结果被送到屏幕,不影响原文件内容

(3)linux是使用GNU版本的grep,它的功能更强,可以通过-G(默认参数)、-E、-F命令行选项来使用egrep和fgrep的功能
grep  -E  <=>  egrep
grep  -F  <=>  fgrep

(4)grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。所以我们利用这些返回值就可进行一些自动化的文本处理工作

2)egrep
egrep命令只跟grep有很小的不同,egrep是grep的扩展,支持更多的re元字符

3)fgrep
fgrep的命令只跟grep有很小的不同, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊

本文转自:http://blog.chinaunix.net/uid-7374279-id-3849705.html


比方以 grep 来说, 在 Linux 上你可找到 grep, egrep, fgrep 这几个程序, 其差异大致如下: 

* grep:
传统的 grep 程序, 在没有参数的情况下, 只输出符合 RE 字符串之句子. 常见参数如下:
-v: 逆反模示, 只输出"不含" RE 字符串之句子. 
-r: 递归模式, 可同时处理所有层级子目录里的文件. 
-q: 静默模式, 不输出任何结果(stderr 除外. 常用以获取 return value, 符合为 true, 否则为 false .)
-i: 忽略大小写. 
-w: 整词比对, 类似 \<word\> . 
-n: 同时输出行号. 
-c: 只输出符合比对的行数.
-l: 只输出符合比对的文件名称.
-o: 只输出符合 RE 的字符串. (gnu 新版独有, 不见得所有版本都支持.)
-E: 切换为 egrep .

* egrep:
为 grep 的扩充版本, 改良了许多传统 grep 不能或不便的操作. 比方说:
- grep 之下不支持 ? 与 + 这两种 modifier, 但 egrep 则可.
- grep 不支持 a|b 或 (abc|xyz) 这类"或一"比对, 但 egrep 则可.
- grep 在处理 {n,m} 时, 需用 \{ 与 \} 处理, 但 egrep 则不需.
诸如此类的... 我个人会建议能用 egrep 就不用 grep 啦... ^_^

* fgrep:
不作 RE 处理, 表达式仅作一般字符串处理, 所有 meta 均失去功能.

 


[ade]表示a,d或e

[^]表示除[]内的字符之外的字符

 

-----------------------实例


在递归目录文件中查找字符串 
grep -r "test" ./

 

 
从/etc/vsftpd/vsftpd.conf  里面 筛选 yes/YES 的行  出来,怎么筛选
grep -i 'yes' /etc/vsftpd/vsftpd.conf

 

grep "chin" 1.txt
查找字符 chin 的行内容(多行)


$grep -n "chin" 1.txt  显示行号
3:    2 chin 102 20040129
5:4 chin 444


grep -c "chin" 1.txt 共出现几行


grep -v "chin" 1.txt  显示所有不包含4 8的各行

在上一例中,抽取字符串“ 4 8”,返回结果包含诸如4 8 4和4 8 3等包含“4 8”的其他字符串,实际上应精确抽取只包含4 8的各行。
使用g r e p抽取精确匹配的一种更有效方式是在抽取字符串后加\ >。假定现在精确抽取4 8,方法如下:
$grep "48\>" data.f


<Tab>表示点击t a b键。
$grep "48<tab>" data.f


grep是大小写敏感的,如要查询大小写不敏感字符串,必须使用- i开关 。

模式范围 grep和正则表达式
$grep "48[34]" data.f
483     Sept    5AP1996 USP     65.00   LVX2C   189
484     nov     7PL1996 CAD     49.00   PLV2C   234
483     may     5PA1998 USP     37.00   KVM9D   644




使行首不是4或8,可以在方括号中使用^记号。
$ grep "^[^48]" data.f
如果是字符串48
$ grep -v "^[^48]" data.f
 


也可以用另一种方式[ ]模式抽取各行包含S e p t和s e p t的所有信息。
$ grep '[sS]ept' data.f

如果要抽取包含S e p t的所有月份,不管其大小写,并且此行包含字符串483,可以使用管道命令,即符号“|”左边命令的输出作为“ |”右边命令的输入。举例如下:
$ grep '[sS]ept' data.f | grep 48



如果抽取以K开头,以D结尾的所有代码,可使用下述方法,因为已知代码长度为5个字符:
grep 'K...D' data.f
47      Oct     3ZL1998 LPSX    43.00   KVM9D   512
483     may     5PA1998 USP     37.00   KVM9D   644


将上述代码做轻微改变,头两个是大写字母,中间两个任意,并以C结尾:
$ grep '[A-Z]..C' data.f
483     Sept    5AP1996 USP     65.00   LVX2C   189
219     dec     2CC1999 CAD     23.00   PLV2C   68
484     nov     7PL1996 CAD     49.00   PLV2C   234



一个常用的查询模式是日期查询。先查询所有以5开始以1 9 9 6或1 9 9 8结尾的所有记录。使用模式5 . . 1 9 9 [ 6 , 8 ]。这意味着第一个字符为5,后跟两个点,接着是1 9 9,剩余两个数字是6或8。
$ grep '5..199[6,8]' data.f
483     Sept    5AP1996 USP     65.00   LVX2C   189
483     may     5PA1998 USP     37.00   KVM9D   644



必须学会使用[ ]抽取信息。假定要取得城市代码,第一个字符为0-9,第二个字符在0到5之间,第三个字符在0到6之间,使用下列模式即可实现。
$ grep '[0-9][0-5[0-6]' data.f



抽取包含数字4至少重复出现两次的所有行,方法如下:
$ grep '4\{2,\}' data.f
483     may     5PA1998 USP     37.00   KVM9D   644

抽取记录使之包含数字9 9 9(三个9),方法如下:
$ grep '9\{3,\}' data.f


$ grep '8\{2,6\}3' myfile
888883
88883


使用grep匹配“与”或者“或”模式
g r e p命令加- E参数,这一扩展允许使用扩展模式匹配。例如,要抽取城市代码为2 1 9或2 1 6,方法如下:
$ grep -E '219|216' data.f
219     dec     2CC1999 CAD     23.00   PLV2C   68
216     sept    3ZL1998 USP     86.00   KVM9E   234



空行
结合使用^和$可查询空行。使用- c参数显示总行数:
$ grep -c '^$' myfile
使用- n参数显示实际在哪一行:
$ grep -c '^$' myfile


查询有特殊含义的字符,诸如$ . ' " * [] ^ | \ + ? ,必须在特定字符前加\。假设要查询包含“.”的所有行,脚本如下:
$ grep '\.' myfile


使用正则表达式可匹配任意文件名。系统中对文本文件有其标准的命名格式。一般最多六个小写字符,后跟句点,接着是两个大写字符。
$ grep '^[a-z]\{1,6\}\.[A-Z]\{1,2\}' filename

--------------------------------------


$ cat /etc/passwd | grep guru
正确的方法应该是:
grep guru /etc/passwd 


------------------------g r e p允许使用国际字符模式匹配或匹配模式的类名形式。
类名及其等价的正则表达式类等价的正则表达式类等价的正则表达式



[ [ : u p p e r : ] ]  ==> [ A - Z ] 
[ [ : a l n u m : ] ]  ==> [ 0 - 9 a - zA-Z]
[ [ : l o w e r : ] ]  ==> [ a - z ] 
[ [ : s p a c e : ] ]  ==> 空格或t a b键
[ [ : d i g i t : ] ]  ==> [ 0 - 9 ] 
[ [ : a l p h a : ] ]  ==> [ a - z A - Z ]


取以5开头,后跟至少两个大写字母:
$grep '5[[:upper:]][[:upper]]' data.f


取以P或D结尾的所有产品代码:
grep '[[:upper:]][[:upper:]][P,D]' data.f


使用通配符*的匹配模式
grep "l.*s" testfile


如在行尾查询某一单词,试如下模式:
grep "ng$" testfile  这将在所有文件中查询行尾包含单词ng的所有行


使用g r e p命令- s开关,可屏蔽错误信息。
如果g r e p命令不支持- s开关,可替代使用以下命令:
# grep "sam" /tec/password >/dev/null 2>&1
脚本含义是匹配命令输出或错误( 2 > $ 1),并将结果输出到系统池。大多数系统管理员称/ d e v / n u l l为比特池,没关系,可以将之看成一个无底洞,有进没有出,永远也不会填满。



如要保存g r e p命令的查询结果,可将命令输出重定向到一个文件。
# grep "sam" /etc/passwd >/usr/sam/passwd.out
# cat /usr/sam/passwd.out
sam:x:506:4::/usr/sam:/bin/bash




g r e p不只应用于文件,也可应用于字符串。为此使用e c h o字符串命令,然后对g r e p命令使用管道输入。
# STR="Mary Joe Peter Pauline"
# echo $STR | grep "Mary"









--------------------egrep代表e x p r e s s i o n或extended grep,适情况而定。
e g r e p接受所有的正则表达式, e g r e p的一个显著特性是可以以一个文件作为保存的字符串,然后将之传给e g r e p作为参数,为此使用- f开关。


创建一个名为g r e p s t r i n g s的文件,并输入4 8 4和4 7:

# vi grepstrings
# cat grepstrings
484
47


egrep -f grepstrings data.f
47      Oct     3ZL1998 LPSX    43.00   KVM9D   512
484     nov     7PL1996 CAD     49.00   PLV2C   234



如果要查询存储代码3 2 L或2 C C,可以使用(|)符号,意即“|”符号两边之一或全部。
$egrep '(3ZL|2CC)' data.f
47      Oct     3ZL1998 LPSX    43.00   KVM9D   512
219     dec     2CC1999 CAD     23.00   PLV2C   68
216     sept    3ZL1998 USP     86.00   KVM9E   234



可以使用任意多竖线符“ |”,例如要查看在系统中是否有帐号l o u i s e、m a t t y或pauline ,使用w h o命令并管道输出至e g r e p。
$who |egrep (louise|matty|pauline)


还可以使用^符号排除字符串。如果要查看系统上的用户,但不包括m a t t y和p a u l i n e,方法如下:
$who |egrep -v '^(matty|pauline)'


如果要查询一个文件列表,包括s h u t d o w n、s h u t d o w n s、r e b o o t和r e b o o t s,使用e g r e p可容易地实现。
$egrep '(shutdown |reboot) (s)?' *


本文转自:http://zhengdl126.iteye.com/blog/801609

[yuhuanxi@localhost postgis-3.2.1]$ make clean ./configure checking for a BSD-compatible install... /usr/bin/install -c checking build system type... x86_64-pc-linux-gnu checking host system type... x86_64-pc-linux-gnu checking how to print strings... printf checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking for a sed that does not truncate output... /usr/bin/sed checking for grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E checking for fgrep... /usr/bin/grep -F checking for ld used by gcc... /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld... yes checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B checking the name lister (/usr/bin/nm -B) interface... BSD nm checking whether ln -s works... yes checking the maximum length of command line arguments... 1572864 checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop checking for /usr/bin/ld option to reload object files... -r checking for objdump... objdump checking how to recognize dependent libraries... pass_all checking for dlltool... no checking how to associate runtime and link libraries... printf %s\n checking for ar... ar checking for archiver @FILE support... @ checking for strip... strip checking for ranlib... ranlib checking for gawk... gawk checking command to parse /usr/bin/nm -B output from gcc object... ok checking for sysroot... no checking for a working dd... /usr/bin/dd checking how to truncate binary pipes... /usr/bin/dd bs=4096 count=1 checking for mt... no checking if : is a manifest tool... no checking how to run the C preprocessor... gcc -E checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for dlfcn.h... yes checking for objdir... .libs checking if gcc supports -fno-rtti -fno-exceptions... no checking for gcc option to produce PIC... -fPIC -DPIC checking if gcc PIC flag -fPIC -DPIC works... yes checking if gcc static flag -static works... no checking if gcc supports -c -o file.o... yes checking if gcc supports -c -o file.o... (cached) yes checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes checking whether -lc should be explicitly linked in... no checking dynamic linker characteristics... GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether stripping libraries is possible... yes checking if libtool supports shared libraries... yes checking whether to build shared libraries... yes checking whether to build static libraries... yes checking for gcc... (cached) gcc checking whether we are using the GNU C compiler... (cached) yes checking whether gcc accepts -g... (cached) yes checking for gcc option to accept ISO C89... (cached) none needed checking how to run the C preprocessor... gcc -E checking for grep that handles long lines and -e... (cached) /usr/bin/grep checking whether byte ordering is bigendian... no checking for cpp... /usr/bin/cpp checking if gcc supports -fno-math-errno... yes checking if gcc supports -fno-signed-zeros... yes checking if gcc supports -std=gnu99... yes checking if gcc supports --exclude-libs... yes checking for flex... no checking for lex... no checking for bison... no checking ieeefp.h usability... no checking ieeefp.h presence... no checking for ieeefp.h... no checking termios.h usability... yes checking termios.h presence... yes checking for termios.h... yes checking for vasprintf... yes checking for asprintf... yes checking for _LARGEFILE_SOURCE value needed for large files... no checking for perl... /usr/bin/perl checking for xsltproc... /usr/bin/xsltproc checking for convert... no configure: WARNING: ImageMagick does not seem to be installed. Documentation cannot be built checking for dblatex... no configure: WARNING: dblatex is not installed so PDF documentation cannot be built checking for xmllint... /usr/bin/xmllint configure: WARNING: could not locate Docbook stylesheets required to build the documentation checking for pkg-config... /usr/bin/pkg-config checking pkg-config is at least version 0.9.0... yes checking for CUNIT... no configure: WARNING: could not locate CUnit required for unit tests checking iconv.h usability... yes checking iconv.h presence... yes checking for iconv.h... yes checking for libiconv_open in -liconv... no checking for iconv_open in -lc... yes checking for iconvctl... no checking for libiconvctl... no checking for pg_config... /usr/bin/pg_config checking PostgreSQL version... PostgreSQL 9.2.24 configure: error: PostGIS requires PostgreSQL >= 9.6 make: *** [GNUmakefile] 错误 1 给出以上报错的解决方式
最新发布
10-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值