Linux三剑客之grep全局搜索正则表达式

发布时间:2015-05-12 17:10:56

grep全局搜索正则表达式

global RE printing(grep)

grep家族有三个:grepegrepfgerp

其中fgerp不支持正则表达式。如果需要搜素字符串,不需要使用元字符,可以用fgerp比较快。

grep 文本过滤工具或文本匹配工具,能够实现根据指定的模式“逐行搜索文件内容,并将匹配到的行显示出来。并不一定是全部匹配。

模式:是由正则表达式的元字符,其他字符组成的匹配条件。

正则表达式(RE)

正则表达式(RE)又称正规表示法、常规表示法,计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。

正则表达式(REGEXP)分为两类:

Basic RE (基本正则表达式)

Extended RE (扩展正则表达式)比基本强大。

命令格式:

grep [option] .. PATTERN 文件名……………. (option,选项的意思PATTERN:模式。)

[option]选项:

--color 显示颜色。

-v 显示没有被模式匹配到的行,取反。

-o 只显示被模式匹配到的字符串。

-i 不考虑大小写。

-n 顺便输出行号。

例子:

[root@en script]# grep -o "root" /etc/passwd

root

root

root

root

[root@en script]#

[root@en ~]# grep 'dwzhang' /etc/passwd

dwzhang:引用变量时,需要用双引号。

[root@en init.d]# grep "\$?" ypbind

retval=$?

retval=$?

retval=$?

retval=$?

retval=$?

exit $?

[root@en init.d]#

例子:

[root@en ~]# grep 'dwzhang' /etc/passwd

dwzhang:x:500:500::/home/dwzhang:/bin/bash

[root@en ~]#

[root@localhost log]# grep 'cpu' /proc/cpuinfo (只显示包含cpu的字符)(-v显示不包含cpu)

cpu family : 6

cpu MHz : 2369.039

cpuid level : 13

[root@localhost log]#

(--color=auto :匹配的字符用颜色高亮显示出来。)

[root@localhost ~]# grep --color=auto 'cpu' /proc/cpuinfo (--color=auto:选项)

cpu family : 6

cpu MHz : 2369.039

cpuid level : 13

[root@localhost ~]#

可以为—color设置链接:

[root@yum ~]# alias grep='grep --color'

[root@yum ~]# grep 'dwzhang' /etc/passwd

dwzhang:x:500:500::/home/dwzhang:/bin/bash

[root@yum ~]#

在关键字的显示方面,grep 可以使用 --color=auto 来将关键字部分使用颜色显示。 这可是个很不错的功能啊!但是如果每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你可以在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦!

[root@en ~]# ifconfig | grep 'inet addr' (只显示inet addr)

inet addr:192.168.20.24 Bcast:192.168.20.255 Mask:255.255.255.0

inet addr:127.0.0.1 Mask:255.0.0.0

[root@en ~]#

(cut -d: -f1表示取第1段,cut切割)

[root@en ~]# grep 'dwzhang' /etc/passwd | cut -d: -f1

dwzhang

[root@en ~]# grep 'dwzhang' /etc/passwd | cut -d: -f2

x

[root@en ~]#

[root@en ~]# ifconfig | grep 'inet addr' | cut -d: -f1

inet addr

inet addr

[root@en ~]# ifconfig | grep 'inet addr' | cut -d: -f2

192.168.20.24 Bcast

127.0.0.1 Mask

[root@en ~]#

[root@en ~]# ifconfig | grep 'inet addr' | cut -d: -f2 | cut -d' ' -f1

192.168.20.24

127.0.0.1

[root@en ~]#

PATTERN 基本正则表达式元字符:

字符匹配:

. :(点)表示匹配任意单个字符。

例子:

[root@en ~]# grep 'c.u' /proc/cpuinfo (匹配c什么p,单个字符)

cpu family : 6

cpu MHz : 3192.829

cpuid level : 13

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb xsaveopt pln pts dts fsgsbase smep

[root@en ~]#

[root@en ~]# grep --color 'c..' /proc/cpuinfo

processor : 0

cpu family : 6

cpu MHz : 3192.788

cache size : 6144 KB

……………………………..

[root@en ~]#

[root@yum ~]# grep 'r..t' /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@yum ~]#

[] :表示匹配指定范围内的任意单个字符。

例子:

[root@en ~]# grep 'c..[a-z]' /proc/cpuinfo (跟了任意2个字符,后面跟了a-z小写字母的)

processor : 0

cache size : 6144 KB

fpu_exception : yes

cpuid level : 13

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb xsaveopt pln pts dts fsgsbase smep

clflush size : 64

cache_alignment : 64

[root@en ~]#

[^] :表示匹配指定范围外的任意单个字符。

[root@en ~]# grep 'a[^[:space:]]*b' abc (^取反,查找非空格的)

ab

accccb

[root@en ~]#

次数匹配:

(默认工作贪婪模式,能匹配多长就多长。)

两个*和?。

* :表示匹配其前的字符0,1或多次;ab*c是指匹配前面字母b多少次,不是指匹配任意字符。如abbcabbbbbbbbbbbcabc=可以0匹配。 不是指abdc

.* :点和星,匹配任意长度的任意字符。

a.*bab之间匹配任意长度的任意字符,没有也可以。

例子:

[root@yum testfile]# cat gr

a

b

ab

aab

acb

avvvb

acv

avjbvkbnhb

[root@yum testfile]# grep 'a*b' gr (表示b之前什么都没有或有a)

b

ab

aab

acb

avvvb

avjbvkbnhb

[root@yum testfile]#

例子:

[root@en ~]# vim abc

ab

a b

a b

a b

a b (Tab制表键)

accccb

[root@en ~]# grep 'a *b' abc (制表键没有找出来) (ab之间只能有a或没有)

ab

a b

a b

a b

[root@en ~]#

[root@en ~]# grep 'a[[:space:]]*b' abc (空格和制表键空出来)

ab

a b

a b

a b

a b

[root@en ~]#

[root@en testold]# grep "a.*b" abc (任意长度的任意字符,a开头b之间的中间任意)

ab

a b

a b

a b

a b

accccb

abc

abcd

abcdef

abbc

abcc

abhcn

[root@en testold]#

? :匹配之前的字符01次。就是说前面的字符可有可无的。

ab?c :不匹配如acabcadcabbc

\{m,n\}:至少m次,至多n次。

\{m,\}:至少m次,多了不限。

\{0,n\}:至多n次。

\{m\}:精确m次。

注意:\为转义符。

锚定符:单词锚定:

词首锚定:

\< :也可用\b表示,锚定词首

例子:匹配\root

this is root 不匹配。

hello,rooter匹配。匹配字符词首,不是行首。

\> :也可用\b表示,锚定词尾。

例子:

[root@yum testfile]# vi gr

acv,this is root

avjbvkbnhb

rooter is adog

the user is mroot

mrooter is not a word.

zhang isroot me

[root@yum testfile]# grep 'root\>' gr (root作为词尾的,注意是词尾,不是行尾)

acv,this is root

the user is mroot

zhang isroot me

[root@yum testfile]#

[root@yum testfile]# grep '\ (root出现词首的)

acv,this is root

rooter is adog

[root@yum testfile]#

[root@yum testfile]# grep '\' gr (精确匹配root,必须是root单词)

acv,this is root

[root@yum testfile]#

[root@yum testfile]# grep 'root\b' gr (也可以用\b表示)

acv,this is root

the user is mroot

zhang isroot me

[root@yum testfile]# grep '\broot\b' gr (也可以用\b表示)

acv,this is root

[root@yum testfile]#

行首锚定:

^ :这个字符必须出现行首,行前不能有空白。

如:^root

hello,root不匹配。

root is me匹配。

[root@yum testfile]# grep '^i' /etc/inittab (i开头的行)

id:5:initdefault:

[root@yum testfile]#

[root@en testold]# grep -n '^ ' aa (以空格开头的)

2: hahaha have wo shi han.

4: /etc/httpd /usr /dev

6: #zhu shi wu xiao

10: zhangdawei

11: dwzhang

13: zhang2

[root@en testold]#

$ :这个字符必须出现行尾。

[root@yum testfile]# grep 'w$' /etc/inittab (行尾为w的行)

# For information on how to write upstart event handlers, or how

[root@yum testfile]#

[root@yum testfile]# grep 'b..h$' /etc/passwd (b什么h结尾的)

root:x:0:0:root:/root:/bin/bash

dwzhang:x:500:500::/home/dwzhang:/bin/bash

zhang1:x:501:501::/home/zhang1:/bin/bash

zhang:x:555:555::/home/zhang:/bin/bash

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

zhangdawei:x:666:666::/home/zhangdawei:/bin/bash

da1:x:667:668::/home/da1:/bin/bash

user1:x:1000:1000::/home/user1:/bin/bash

mandriva:x:4004:3004::/home/mandriva:/bin/bash

fedora:x:2003:2003:fedora community:/home/fedora:/bin/bash

openstack:x:4005:4005::/home/openstack:/bin/bash

[root@yum testfile]#

查找匹配dwzhang词首前面有n次空格的:

^[[:space:]]*dwzhang

[root@en testold]# grep '^ *zhangdawei' aa

zhangdawei

[root@en testold]#

或:

[root@en testold]# grep '^[[:space:]]*zhangdawei' aa

zhangdawei

[root@en testold]#

分组字符:

可以将几个字符当成一组来使用。

\(\)

比如:

\(ab\)*:表示ab可以出现任意次。

向引用:

\1 :表示引用第一个左括号以及与之对应的右括号所包括的所有内容。

\2 :表示引用第二个左括号以及与之对应的右括号所包括的所有内容。

\3 :表示引用第三个左括号以及与之对应的右括号所包括的所有内容。

4\5\,……………..以此类推。

比如:有这么几行:

He love his lover.

She like her liker.

He like his lover.

She love her liker.

She like him.

找出每行前面有一个字符串,后面一个字符串跟r的字符:

[root@en testold]# grep 'l..e.*l..e' aa (查找l..e任意字符l..e)

He love his lover.

She like her liker.

He like his lover.

She love her liker.

[root@en testold]# grep 'l..e' aa (查找有l..e字符的)

He love his lover.

She like her liker.

He like his lover.

She love her liker.

She like him.

[root@en testold]#

[root@en testold]# grep '\(l..e\).*\1' aa (查找l..e跟任意字符后面有第一个括号里面的字符)

He love his lover.

She like her liker.

[root@en testold]#

[root@en testold]# grep '\(love\).*\1' aa (查找love跟任意字符后面有第一个括号里面的字符)

He love his lover.

[root@en testold]#

例子:

查找inittab文件行中出现一个任意数字,要求结尾也出现了这个数字:

[root@en testold]# grep -E '([0-9]).*\1$' inittab (前面出现数字跟一任意字符后面出现括号一样的1\表示引用第一个括号里面的字符)

# 5 - X11

l0:0:wait:/etc/rc.d/rc 0

l1:1:wait:/etc/rc.d/rc 1

l2:2:wait:/etc/rc.d/rc 2

l3:3:wait:/etc/rc.d/rc 3

l4:4:wait:/etc/rc.d/rc 4

l5:5:wait:/etc/rc.d/rc 5

l6:6:wait:/etc/rc.d/rc 6

1:2345:respawn:/sbin/mingetty tty1

2:2345:respawn:/sbin/mingetty tty2

3:2345:respawn:/sbin/mingetty tty3

4:2345:respawn:/sbin/mingetty tty4

5:2345:respawn:/sbin/mingetty tty5

6:2345:respawn:/sbin/mingetty tty6

[root@en testold]#

或:

[root@en testold]# grep '\([0-9]\).*\1$' inittab

# 5 - X11

l0:0:wait:/etc/rc.d/rc 0

l1:1:wait:/etc/rc.d/rc 1

l2:2:wait:/etc/rc.d/rc 2

l3:3:wait:/etc/rc.d/rc 3

l4:4:wait:/etc/rc.d/rc 4

l5:5:wait:/etc/rc.d/rc 5

l6:6:wait:/etc/rc.d/rc 6

1:2345:respawn:/sbin/mingetty tty1

2:2345:respawn:/sbin/mingetty tty2

3:2345:respawn:/sbin/mingetty tty3

4:2345:respawn:/sbin/mingetty tty4

5:2345:respawn:/sbin/mingetty tty5

6:2345:respawn:/sbin/mingetty tty6

[root@en testold]#

例子:

显示inittab文件中以一个数字开头并以一个与开头数字相同的数字结尾的行:

[root@en testold]# grep '^\([0-9]\).*\1$' inittab

1:2345:respawn:/sbin/mingetty tty1

2:2345:respawn:/sbin/mingetty tty2

3:2345:respawn:/sbin/mingetty tty3

4:2345:respawn:/sbin/mingetty tty4

5:2345:respawn:/sbin/mingetty tty5

6:2345:respawn:/sbin/mingetty tty6

[root@en testold]#

grep默认只支持使用基本正则表达式定义的模式来过滤文本的命令。

grep选项:(man grep)

-v显示不被模式匹配的行。

-i不区分字符大小写。

-o只显示匹配的串。

--color加颜色。

-E使用扩展正则表达式。

-A-A 数字)显示匹配,并显示匹配下面的2行。

-B并显示匹配上面的2行。

-C并显示匹配上面和下面的2行。

例子:[root@en ~]# grep '^fpu' /proc/cpuinfo

fpu : yes

fpu_exception : yes

[root@en ~]# grep -A 2 '^fpu' /proc/cpuinfo (显示匹配的行和下面的2)

fpu : yes

fpu_exception : yes

cpuid level : 13

wp : yes

[root@en ~]#

[root@en ~]# grep --color -B 2 '^fpu' /proc/cpuinfo (显示匹配的行和上面的2)

cpu MHz : 3192.829

cache size : 6144 KB

fpu : yes

fpu_exception : yes

[root@en ~]# grep --color -C 2 '^fpu' /proc/cpuinfo (显示匹配的行和上下面的2)

cpu MHz : 3192.829

cache size : 6144 KB

fpu : yes

fpu_exception : yes

cpuid level : 13

wp : yes

[root@en ~]#

显示文件去除掉空白行:

[root@en ~]# grep -v "^$" /etc/httpd/conf/httpd.conf

显示文件去除掉#注释行:

[root@en ~]# grep -v "#" /etc/httpd/conf/httpd.conf

显示去除掉注释行和空白行:

[root@en ~]# grep -v "#" /etc/httpd/conf/httpd.conf | grep -v "^$"

扩展正则表达式:egrep(需要使用-E)

grep E等于egrep

字符匹配:(和基本一样)

. (点)表示匹配任意单个字符。

[] :表示匹配指定范围内的任意单个字符。

[^] :表示匹配指定范围外的任意单个字符。

次数匹配:

* 表示匹配其前的字符0,1或多次;

? :匹配之前的字符01次。就是说前面的字符可有可无的。

+ :匹配其前面的字符至少1次。

{m,n} :和基本正则表达式不同,不需要使用反斜线\至少m次,至多n次。

\<锚定词首

\>:锚定词尾。

^:这个字符必须出现行首,行前不能有空白。

$:这个字符必须出现行尾。

分组:

() :分组。

\1\2\3……

例如:

[root@en ~]# ifconfig | grep -E "1(92|27)" (192127)

inet addr:192.168.20.24 Bcast:192.168.20.255 Mask:255.255.255.0

inet addr:127.0.0.1 Mask:255.0.0.0

[root@en ~]#

如果不用扩展正则表达式:需要加转义符。

[root@en ~]# ifconfig | grep "1\(92\|27\)"

inet addr:192.168.20.24 Bcast:192.168.20.255 Mask:255.255.255.0

inet addr:127.0.0.1 Mask:255.0.0.0

[root@en ~]#

或者:

a|b a或者b,表示or的意思。

例子:

[root@en ~]# vim abc

abcc

abhcn

cat

C

Cat

China

[root@en ~]# grep --color -E 'C|cat' abc (匹配大C或者匹配cat)

cat

C

Cat

China

[root@en ~]#

[root@en ~]# grep --color -E '(C|c)at' abc (用分组,匹配c大或者小写cat)

cat

Cat

[root@en ~]#

[root@en ~]# grep --color -E '^[[:space:]]+' /boot/grub/grub.conf

root (hd0,1) (匹配至少一个空白字符)

[root@en ~]#

正则表达式练习题:

1. 找出/proc/cpuinfo文件中的1位数或2位数?

[root@en ~]# grep -E "\<[0-9]{1,2}\>" /proc/cpuinfo ({1,2}至少1个,至多2)

或:

[root@en ~]# grep --color '\<[0-9]\{1,2\}\>' /proc/cpuinfo

processor : 0

cpu family : 6

model : 58

model name : Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz

stepping : 9

cpuid level : 13

bogomips : 6385.65

clflush size : 64

cache_alignment : 64

address sizes : 40 bits physical, 48 bits virtual

[root@en ~]#

至少1位最多3位数:

[root@en testold]# grep -E "\<[0-9]{1,3}\>" /proc/cpuinfo

processor : 0

cpu family : 6

model : 58

model name : Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz

stepping : 9

cpu MHz : 3192.789

cpuid level : 13

bogomips : 6385.57

clflush size : 64

cache_alignment : 64

address sizes : 40 bits physical, 48 bits virtual

[root@en testold]#

2. 找出ifconfig命令结果中的1-255之间的整数?

[root@en testold]# ifconfig | egrep --color '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

eth0 Link encap:Ethernet HWaddr 00:0C:29:A1:01:C7

inet addr:192.168.20.24 Bcast:192.168.20.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fea1:1c7/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX bytes:28669748 (27.3 MiB) TX bytes:935742 (913.8 KiB)

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX bytes:18858 (18.4 KiB) TX bytes:18858 (18.4 KiB)

[root@en testold]#

说明:

\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\> :

\<\> :必须是这些数。

()表示锚定整个括号里面的,不加只锚定[1-9]| 或之前的。

| :代表

[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] :第一位数分解:

[1-9]| [1-9][0-9]| 1[0-9][0-9]| 2[0-4][0-9]| 25[0-5]

1-9 10-99 100-199 200-249 250-255

找出ifconfig命令里面的IP地址与子网:

[root@en testold]# ifconfig | egrep --color '\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

inet addr:192.168.20.24 Bcast:192.168.20.255 Mask:255.255.255.0

inet addr:127.0.0.1 Mask:255.0.0.0

[root@en testold]#

或:

[root@en testold]# ifconfig | egrep --color '(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

inet addr:192.168.20.24 Bcast:192.168.20.255 Mask:255.255.255.0

inet addr:127.0.0.1 Mask:255.0.0.0

[root@en testold]#

为什么上面重复3次不是4次,因为IP地址有点,3个点。

3. 查找当前系统上名字zhang(必须出现在行首)的用户账号的相关信息?

[root@en ~]# cat /etc/passwd

zhang1:x:501:501::/home/zhang1:/bin/bash

zhang:x:502:502::/home/zhang:/bin/bash

[root@en ~]#

[root@en ~]# grep '^zhang\>' /etc/passwd | cut -d: -f3

502

[root@en ~]# grep '^zhang\>' /etc/passwd | cut -d: -f1

zhang

[root@en ~]#

4. 分析/etc/inittab文件中如下文本中前两行的特征(每行出现的数字相同)

[root@en testold]# grep '^l\([0-9]\):\1.*\1$' inittab (开头不是数字1,是字母l)

l0:0:wait:/etc/rc.d/rc 0

l1:1:wait:/etc/rc.d/rc 1

l2:2:wait:/etc/rc.d/rc 2

l3:3:wait:/etc/rc.d/rc 3

l4:4:wait:/etc/rc.d/rc 4

l5:5:wait:/etc/rc.d/rc 5

l6:6:wait:/etc/rc.d/rc 6

[root@en testold]#

5. 查找inittab文件至少有一个空白字符开头的行:

[root@en testold]# grep --color -E '^[[:space:]]+' inittab

# hi world

# 0 - halt (Do NOT set initdefault to this)#

# 1 - Single user mode

# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)

# 3 - Full multiuser mode

# 4 - unused

# 5 - X11

[root@en testold]#

或:

[root@en testold]# grep '^[[:space:]]\{1,\}' inittab

# hi world

# 0 - halt (Do NOT set initdefault to this)#

# 1 - Single user mode

# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)

# 3 - Full multiuser mode

# 4 - unused

# 5 - X11

[root@en testold]#

简介

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

Unixgrep家族包括grepegrepfgrepegrepfgrep的命令只跟grep有很小不同。egrepgrep的扩展,支持更多的re元字符, fgrep就是fixed grepfast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G-E-F命令行选项来使用egrepfgrep的功能。

grep常用用法

[root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename

选项与参数:

-a binary 文件以 text 文件的方式搜寻数据

-c 计算找到 '搜寻字符串' 的次数

-i 忽略大小写的不同,所以大小写视为相同

-n 顺便输出行号

-v 反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!

--color=auto 可以将找到的关键词部分加上颜色的显示喔!

例如:

/etc/passwd,有出现 root 的行取出来:

[root@en ~]# grep 'root' /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[root@en ~]#

或者:

[root@en ~]# cat /etc/passwd | grep 'root'

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[root@en ~]#

/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号:

[root@en ~]# grep -n 'root' /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

11:operator:x:11:0:operator:/root:/sbin/nologin

[root@en ~]#

其他:

在关键字的显示方面,grep 可以使用 --color=auto 来将关键字部分使用颜色显示。 这可是个很不错的功能啊!但是如果每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你可以在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦

/etc/passwd,将没有出现 root 的行取出来:

[root@en ~]# grep -v 'root' /etc/passwd (-v是取反)

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

………………………………

[root@en ~]#

/etc/passwd,将没有出现 root nologin的行取出来:

[root@en ~]# grep -v 'root' /etc/passwd | grep -v 'nologin'

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

………………………………..

[root@en ~]#

dmesg 列出核心信息,再以 grep 找出内含 eth 那行,要将捉到的关键字显色,且加上行号来表示:

[root@en ~]# dmesg | grep -n --color 'eth'

1596:e1000 0000:02:01.0: eth0: (PCI:66MHz:32-bit) 00:0c:29:a1:01:c7

1597:e1000 0000:02:01.0: eth0: Intel(R) PRO/1000 Network Connection

1615:e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None

1616:8021q: adding VLAN 0 to HW filter on device eth0

[root@en ~]#

# 你会发现除了 eth 会有特殊颜色来表示之外,最前面还有行号喔!

在关键字的显示方面,grep 可以使用 --color=auto 来将关键字部分使用颜色显示。 这可是个很不错的功能啊!但是如果每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你可以在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦

dmesg 列出核心信息,再以 grep 找出内含 eth 那行,在关键字所在行的前两行与后三行也一起捉出来显示:

[root@en ~]# dmesg | grep -n -A3 -B2 "eth"

1594-e1000 0000:02:01.0: PCI INT A -> GSI 19 (level, low) -> IRQ 19

1595-e1000 0000:02:01.0: setting latency timer to 64

1596:e1000 0000:02:01.0: eth0: (PCI:66MHz:32-bit) 00:0c:29:a1:01:c7

1597:e1000 0000:02:01.0: eth0: Intel(R) PRO/1000 Network Connection

1598-parport_pc 00:09: reported by Plug and Play ACPI

1599-parport0: PC-style at 0x378, irq 7 [PCSPP,TRISTATE]

1600-ENS1371 0000:02:02.0: PCI INT A -> GSI 16 (level, low) -> IRQ 16

--

1613-802.1Q VLAN Support v1.8 Ben Greear

1614-All bugs added by David S. Miller

1615:e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None

1616:8021q: adding VLAN 0 to HW filter on device eth0

1617-SELinux: initialized (dev autofs, type autofs), uses genfs_contexts

1618-SELinux: initialized (dev autofs, type autofs), uses genfs_contexts

1619-SELinux: initialized (dev autofs, type autofs), uses genfs_contexts

[root@en ~]#

# 如上所示,你会发现关键字 1596 所在的前两行及 1616 后三行也都被显示出来!

# 这样可以让你将关键字前后数据捉出来进行分析啦!

根据文件内容递归查找目录:

[root@en testold]# grep 'zhang' * (在当前目录搜索带'zhang'行的文件,仅在当前目录下)

aa: zhangdawei

aa: dwzhang

aa: zhang1

aa: zhang2

abc:heoo,nihao,zhang,wol

lk:hello zhang

op:wo shi. zhang1a.

op:wo shi. zhang2a.

op:wo shi. zhang3a.

test2:hello zhang

test3:hello zhang

[root@en testold]#

[root@en testold]# grep -r 'zhang' * (-r:在当前目录及其子目录下搜索'zhang'行的文件)

aa: zhangdawei

aa: dwzhang

aa: zhang1

aa: zhang2

abc:heoo,nihao,zhang,wol

lk:hello zhang

op:wo shi. zhang1a.

op:wo shi. zhang2a.

op:wo shi. zhang3a.

test2:hello zhang

test3:hello zhang

zh/zg:zhang is ok

[root@en testold]#

-r l :在当前目录及其子目录下搜索'zhang'行的文件,但是不显示匹配的行,只显示匹配的文件

[root@en testold]# grep -r -l 'zhang' *

aa

abc

lk

op

test2

test3

zh/zg

[root@en testold]#

grep与正则表达式

字符类:

字符类的搜索:

[ ] :中括号。

如果我想要搜寻 this thes 这两个单字时,可以发现到,其实她们有共通的 'th?s' 存在~这个时候,我可以这样来搜寻:

[root@en testold]# grep -n 'th[ie]s' aa

1:/this is aa

8:find this is ok!

19:find thes a fine!

[root@en testold]#

[root@en testold]# grep -n 'th[ieu]s' aa (包括thisthesthus都可以)

1:/this is aa

8:find this is ok!

19:find thes a fine!

20:thus are me!

[root@en testold]#

其实 [] 里面不论有几个字节,他都谨代表某『一个』字节, 所以,上面的例子说明了,我需要的字串是『this』或『thes』,[thus]字串而已!

[^] :取中括号里面反的。

字符类的反向选择 [^]

如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下:

[root@en testold]# grep -n '[^g]oo' aa

21:apple is my favorite food.

22:Football game is not use feet only.

23:google is the best tools for search keyword. (把改行打印出来)

24:goooooogle yes!

25:gooogle NO!

[root@en testold]#

21,22, 行没有疑问,因为 foo Foo too均可被接受!

但是第 23 行明明有 google goo 啊~别忘记了,因为该行后面出现了 tool too 啊!所以该行也被列出来~ 也就是说, 18 行里面虽然出现了我们所不要的项目 (goo) 但是由于有需要的项目 (too) 因此,是符合字串搜寻的喔!

至于第 24 行,同样的,因为 goooooogle 里面的 oo 前面可能是 o ,例如: go(ooo)oogle ,所以,这一行也是符合需求的!

字符类的连续:

再来,假设我 oo 前面不想要有小写字节,所以,我可以这样写 [^abcd....z]oo 但是这样似乎不怎么方便,由于小写字节的 ASCII 上编码的顺序是连续的, 因此,我们可以将之简化为底下这样:

[root@en testold]# grep -n '[^a-z]oo' aa

22:Football game is not use feet only.

[root@en testold]#

也就是说,当我们在一组集合字节中,如果该字节组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9]等方式来书写,那么如果我们的要求字串是数字与英文呢? 呵呵!就将他全部写在一起,变成:[a-zA-Z0-9]

我们要取得有数字的那一行,就这样:

[root@en testold]# grep -n '[0-9]' aa

12: zhang1

13: zhang2

26:However, this dress is about $ 3183 dollars.

[root@en testold]#

行首与行尾字节: ^ $

行首字符:

如果我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!我们可以这样做:

[root@en testold]# grep -n '^the' aa

27:the symbol * is represented as start.

[root@en testold]#

此时,就只剩下第 27 行,因为只有第 27 行的行首是 the 开头啊~

此外, 如果我想要开头是小写字节的那一行就列出呢?可以这样:

[root@en testold]# grep '^[a-z]' aa (注意:^[a-z][^a-z]的区别)

hello!

ok!thINK.

find this is ok!

[root@en testold]#

如果我不想要开头是英文字母,则可以是这样:

[root@en testold]# grep -n '^[^a-zA-Z]' aa

1:/this is aa

4: this /etc/httpd /usr /dev

6: #zhu shi wu xiao

[root@en testold]#

注意:^ 符号,在字符类符号(中括号[])之内与之外是不同的! [] 内代表『反向选择』,在 [] 之外则代表定位在行首的意义!

那如果我想要找出来,行尾结束为小数点 (.) 的那一行:

[root@en testold]# grep '\.$' aa

hahaha have wo shi han.

ok!thINK.

zhe shi shou hang.

apple is my favorite food.

Football game is not use feet only.

google is the best tools for search keyword.

However, this dress is about $ 3183 dollars.

the symbol * is represented as start.

[root@en testold]#

特别注意到,因为小数点具有其他意义(底下会介绍),所以必须要使用转义字符(\)来加以解除其特殊意义!

找出空白行:

[root@en testold]# grep -n '^$' aa

15:

18:

[root@en testold]#

因为只有行首跟行尾 (^$),所以,这样就可以找出空白行啦!

任意一个字节 . ()与重复字节 *()

这两个符号在正则表达式的意义如下:

. (小数点)代表『一定有一个任意字节』的意思;

* (星号)代表『重复前一个字符, 0 到无穷多次』的意思,为组合形态。

假设我需要找出 g??d 的字串,共有四个字节, 起头是 g 而结束是 d ,我可以这样做:

[root@en testold]# grep -n 'g..d' aa

28:"Open Source" is a good mechanism to develop programs.

29:Oh! The soup taste good.

30:The world is the same with "glad".

[root@en testold]#

因为强调 g d 之间一定要存在两个字节,因此, god gd 就不会被列出来啦!

如果我想要列出有 oo, ooo, oooo 等等的数据, 也就是说,至少要有两个() o 以上,该如何是好?

当我们需要『至少两个 o 以上的字串』时,就需要 ooo* ,即是:

[root@en testold]# grep -n 'ooo*' aa

21:apple is my favorite food.

22:Football game is not use feet only.

23:google is the best tools for search keyword.

24:goooooogle yes!

25:gooogle NO!

28:"Open Source" is a good mechanism to develop programs.

29:Oh! The soup taste good.

[root@en testold]#

因为 * 代表的是『重复 0 或多个前面的 RE 字符』的意义, 因此,『o*』代表的是:『拥有空字节或一个 o 以上的字节』,因此,『 grep -n 'o*' aa 』将会把所有的数据都列印出来终端上!包括不带o字符的。

如果我想要字串开头与结尾都是 g,但是两个 g 之间仅能存在至少一个 o ,即是 gog, goog, gooog.... 等等,那该如何?

[root@en testold]# grep -n 'goo*g' aa

23:google is the best tools for search keyword.

24:goooooogle yes!

25:gooogle NO!

[root@en testold]#

如果我想要找出 g 开头与 g 结尾的行,当中的字符可有可无:

[root@en testold]# grep -n 'g.*g' aa

23:google is the best tools for search keyword.

24:goooooogle yes!

25:gooogle NO!

28:"Open Source" is a good mechanism to develop programs.

31:go! go! Let's go.

[root@en testold]#

因为是代表 g 开头与 g 结尾,中间任意字节均可接受,所以,第 23,24,25,28,31 行是可接受的喔! 这个 .* RE 表示任意字符是很常见的.

如果我想要找出『任意数字』的行?因为仅有数字,所以就成为:

[root@en testold]# grep -n '[0-9]' aa

12::zhang1

13: zhang2

26:However, this dress is about $ 3183 dollars.

32:You are the best is mean you are the no. 1.

[root@en testold]#

如果我想要限制一个范围区间内的重复字节数呢?

\{m,n\}:至少m次,至多n次。

\{m,\}:至少m次,多了不限。

\{0,n\}:至多n次。

\{m\}:精确m次。

举例来说,我想要找出两个到五个 o 的连续字串,该如何作?

这时候就得要使用到限定范围的字符 {} 了。 但因为 { } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符 \ 来让他失去特殊意义才行。 至于 {} 的语法是这样的,假设我要找到两个 o 的字串,可以是:

[root@en testold]# grep -n 'o\{2\}' aa

21:apple is my favorite food.

22:Football game is not use feet only.

23:google is the best tools for search keyword.

24:goooooogle yes!

25:gooogle NO!

28:"Open Source" is a good mechanism to develop programs.

29:Oh! The soup taste good.

[root@en testold]#

或:

[root@en testold]# grep -E -n 'o{2}' aa

21:apple is my favorite food.

22:Football game is not use feet only.

23:google is the best tools for search keyword.

24:goooooogle yes!

25:gooogle NO!

28:"Open Source" is a good mechanism to develop programs.

29:Oh! The soup taste good.

[root@en testold]#

假设我们要找出 g 后面接 2 5 o ,然后再接一个 g 的字串,他会是这样:

[root@en testold]# grep -n 'go\{2,5\}g' aa

23:google is the best tools for search keyword.

25:gooogle NO!

[root@en testold]#

如果我想要的是 2 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:

[root@en testold]# grep -n 'go\{2,\}g' aa

23:google is the best tools for search keyword.

24:goooooogle yes!

25:gooogle NO!

[root@en testold]#

扩展grep(grep -E 或者 egrep)

使用扩展grep的主要好处是增加了额外的正则表达式元字符集。

打印所有包含NWEA的行。如果不是使用egrep,而使用grep,将需要使用转义符

[root@en testold]# egrep 'NW|EA' aa

northwest NW Charles Main 3.0 .98 3 34

eastern EA TB Savage 4.4 .84 5 20

[root@en testold]#

对于标准grep,如果在扩展元字符前面加\grep会自动启用扩展选项-E

[root@en testold]# grep 'NW\|EA' aa

northwest NW Charles Main 3.0 .98 3 34

eastern EA TB Savage 4.4 .84 5 20

[root@en testold]#

搜索所有包含一个或多个有数字3的行:

[root@en testold]# egrep '3+' aa

However, this dress is about $ 3183 dollars.

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

northeast NE AM Main Jr. 5.1 .94 3 13

central CT Ann Stephens 5.7 .94 5 13

[root@en testold]# grep '3\+' aa

However, this dress is about $ 3183 dollars.

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

northeast NE AM Main Jr. 5.1 .94 3 13

central CT Ann Stephens 5.7 .94 5 13

[root@en testold]#

搜索首先含有数字2字符,其后紧跟着0个或1个点(点没有或只能有一个),后面再是09之间的数字:

[root@en testold]# egrep '2\.?[0-9]' aa

eastern EA TB Savage 4.4 .84 5 20

western WE Sharon Gray 5.3 .97 5 23

southwest SW Lewis Dalsass 2.7 .8 2 18

[root@en testold]#

或者:

[root@en testold]# grep '2\.\?[0-9]' aa

eastern EA TB Savage 4.4 .84 5 20

western WE Sharon Gray 5.3 .97 5 23

southwest SW Lewis Dalsass 2.7 .8 2 18

[root@en testold]#

搜索一个或者多个连续的no的行:

[root@en testold]# egrep '(no)+' aa

Football game is not use feet only.

You are the best is mean you are the no. 1.

northwest NW Charles Main 3.0 .98 3 34

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

[root@en testold]#

fgrep

不使用正则表达式。

fgrep 查询速度比grep命令快,但是不够灵活:它只能找固定的文本,而不是规则表达式。

如果你想在一个文件或者输出中找到包含星号字符的行:

[root@en testold]# fgrep '*' aa

the symbol * is represented as start.

[root@en testold]# grep -F '*' aa (grep F等于fgrep)

the symbol * is represented as start.

[root@en testold]# grep '*' aa

the symbol * is represented as start.

[root@en testold]#

grep练习题:

文档内容:

[root@en greplianxiti]# cat datefile

Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300

Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500

Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400

Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700

Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900

Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100

Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100

Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900

Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900

Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200

Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500

Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700

James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000

Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500

Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600

William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500

Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500

Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000

Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500

Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000

Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350

Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600

Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200

Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000

Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

[root@en greplianxiti]#

1.显示所有包含San的行

[root@en greplianxiti]# grep -n 'San' datefile

6:Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100

20:Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000

24:Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600

[root@en greplianxiti]#

2.显示所有以大写J开始的人名所在的行

[root@en greplianxiti]# grep -n '^J' datefile

5:Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900

6:Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100

15:James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000

20:Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000

24:Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600

[root@en greplianxiti]#

3.显示所有以700结尾的行

[root@en greplianxiti]# grep -n '700$' datefile

4:Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700

14:Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700

[root@en greplianxiti]#

4.显示所有不包括834的行

[root@en greplianxiti]# grep -nv '834' datefile

1:Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300

2:Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500

…………………….

[root@en greplianxiti]#

5.显示所有生日在December(12月份)的行

[root@en greplianxiti]# grep -n ':12/' datefile

15:James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000

16:Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500

[root@en greplianxiti]#

6.显示所有电话号码的区号为284的行

[root@en greplianxiti]# grep -n ':284-' datefile

7:Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100

8:Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

9:Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

[root@en greplianxiti]#

7.显示所有这样的行:它包含一个大写字母,后跟三个小写字母,一个冒号和一个数字

[root@en greplianxiti]# grep -n '[A-Z][a-z][a-z][a-z]:[0-9]' datefile

2:Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500

18:William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500

20:Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000

[root@en greplianxiti]#

或:

[root@en greplianxiti]# grep -n '[A-Z][a-z]\{3\}:[0-9]' datefile

2:Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500

18:William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500

20:Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000

[root@en greplianxiti]#

\{m\}:匹配前面的精确m次,注意:\为转义符。

8.显示姓以Kk开头的行

[root@en greplianxiti]# grep -n '[a-z]\{1\} [Kk]' datefile

16:Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500

17:Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600

18:William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500

[root@en greplianxiti]#

说明:

'[a-z]\{1\} [Kk]' :前面一个小写字母,至少一个,然后是跟着空格,后面是大写K或小写K

9.显示工资为六位数的行,并在前面加行号

[root@en greplianxiti]# grep -n '[0-9]\{6\}$' datefile

4:Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700

10:Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900

11:Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900

13:Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500

16:Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500

22:Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000

[root@en greplianxiti]#

10.显示包括Lincolnlincoln的行,并且grep对大小写不敏感

[root@en greplianxiti]# grep -n -E '(L|l)incoln' datefile

7:Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100

8:Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

9:Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

[root@en greplianxiti]#

或:

[root@en greplianxiti]# grep -ni 'lincoln' datefile

7:Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100

8:Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

9:Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

[root@en greplianxiti]#

练习:

当前目录文件:

[root@en greplianxiti]# ls

awkfile datefile grub.conf ifcfg-eth0 inittab meminfo passwd

[root@en greplianxiti]#

1、显示meminfo文件中以不区分大小s开头的行;

[root@en greplianxiti]# grep -in '^s' meminfo

5:SwapCached: 0 kB

14:SwapTotal: 4095992 kB

15:SwapFree: 4095992 kB

20:Shmem: 1232 kB

21:Slab: 112796 kB

22:SReclaimable: 54600 kB

23:SUnreclaim: 58196 kB

[root@en greplianxiti]#

2、显示passwd中以nologin结尾的行;

[root@en greplianxiti]# grep -n 'nologin$' passwd

2:bin:x:1:1:bin:/bin:/sbin/nologin

3:daemon:x:2:2:daemon:/sbin:/sbin/nologin

4:adm:x:3:4:adm:/var/adm:/sbin/nologin

5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

…………………………..

[root@en greplianxiti]#

3、显示inittab中以#开头,且后面跟一个或多个空白字符,而后又跟了数字的行;

[root@en greplianxiti]# grep -n '^#[[:space:]]\{1,\}[0-9]' inittab

18:# 0 - halt (Do NOT set initdefault to this)

19:# 1 - Single user mode

20:# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)

21:# 3 - Full multiuser mode

22:# 4 - unused

23:# 5 - X11

24:# 6 - reboot (Do NOT set initdefault to this)

[root@en greplianxiti]#

其他:

显示inittab中以#开头,且后面跟一个或多个空白字符,而后又跟了任意字符的行;

[root@en greplianxiti]# grep -n '^#[[:space:]]\{1,\}[a-zA-Z]' inittab

4、显示inittab中包含了:一个数字:(即两个冒号中间一个数字)的行;

[root@en greplianxiti]# grep -n ':[0-9]:' inittab

26:id:5:initdefault:

[root@en greplianxiti]#

5、显示grub.conf文件中以一个或多个空白字符开头的行;

[root@en greplianxiti]# grep -n '^[[:space:]]\{1,\}' grub.conf

6、显示inittab文件中以一个数字开头并以一个与开头数字相同的数字结尾的行;

[root@en greplianxiti]# grep -n '^\([0-9]\).*\1$' inittab

29:1:2345:respawn:/sbin/mingetty tty1

30:2:2345:respawn:/sbin/mingetty tty2

31:3:2345:respawn:/sbin/mingetty tty3

32:4:2345:respawn:/sbin/mingetty tty4

33:5:2345:respawn:/sbin/mingetty tty5

34:6:2345:respawn:/sbin/mingetty tty6

[root@en greplianxiti]#

说明:

\1 :表示引用第一个左括号以及与之对应的右括号内所包括的所有内容。

7ifconfig命令可以显示当前主机的IP地址相关的信息等,如果使用grep等文本处理命令取出本机的各IP地址,要求不包括127.0.0.1

[root@en greplianxiti]# ifconfig eth0 | grep 'inet addr' | cut -d ':' -f2 | cut -d ' ' -f1

192.168.20.24

[root@en greplianxiti]#

其他:

ifconfig命令,这里有eth0loeth1网卡:

[root@en greplianxiti]# ifconfig | grep 'inet addr' | grep -v '127.0.0.1' | cut -d ':' -f2 | cut -d ' ' -f1

192.168.20.24

192.168.1.100

[root@en greplianxiti]#

8、显示ifcfg-eth0文件中的包含了类似IP地址点分十进制数字格式的行;

[root@en greplianxiti]# grep -n '[1-9]\{1,3\}\.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}' ifcfg-eth0

8:IPADDR=192.168.20.24

9:NETMASK=255.255.255.0

10:DNS2=8.8.8.8

11:GATEWAY=192.168.20.1

12:DNS1=192.168.20.2

[root@en greplianxiti]#

Linux三剑客之grep全局搜索正则表达式

相关推荐