[size=large]grep[/size]
grep一般格式为:
grep [选项]基本正则表达式[文件]
这里基本正则表达式可为字符串。
[b]grep选项[/b]
常用的g r e p选项有:
-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符) 。
-h 查询多文件时不显示文件名。
-l 查询多文件时只输出包含匹配字符的文件名。
-n 显示匹配行及行号。
-s 不显示不存在或无匹配文本的错误信息。
-v 显示不包含匹配文本的所有行。
[b]3.grep允许使用国际字符模式匹配或匹配模式的类名形式。[/b]
类名及其等价的正则表达式
查询列表中的目录
查找包含'Oc'的行
显示满足匹配行的所有行的行数
显示不包含匹配行的行
'.'代表一个任意字符,查看包含'M.r'的行
[size=large][b]egrep[/b][/size]
egrep代表expression或extended grep,适情况而定。egrep接受所有的正则表达式,egrep的一个显著特性是可以以一个文件作为保存的字符串,然后将之传给egrep作为参数,为此使用- f开关。
grep一般格式为:
grep [选项]基本正则表达式[文件]
这里基本正则表达式可为字符串。
[b]grep选项[/b]
常用的g r e p选项有:
-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符) 。
-h 查询多文件时不显示文件名。
-l 查询多文件时只输出包含匹配字符的文件名。
-n 显示匹配行及行号。
-s 不显示不存在或无匹配文本的错误信息。
-v 显示不包含匹配文本的所有行。
[b]3.grep允许使用国际字符模式匹配或匹配模式的类名形式。[/b]
类名及其等价的正则表达式
类 等价的正则表达式 类 等价的正则表达式
[ [ : 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 ]
查询列表中的目录
/home/l/g/tomotoboy >ls -al|grep '^d'
drwx------ 8 tomotoboy member 4096 Aug 7 13:45 .
drwxr-xr-x 202 root root 4096 Oct 21 2008 ..
drwxr-xr-x 3 tomotoboy member 4096 Oct 20 2008 .sunstudio
drwxr-xr-x 2 tomotoboy member 4096 Aug 6 16:26 2
drwxr-xr-x 2 tomotoboy member 4096 Aug 7 19:16 c
drwxr-xr-x 2 tomotoboy member 4096 Jul 29 14:08 main
drwxr-xr-x 2 tomotoboy member 4096 Oct 21 2008 seawolf
drwxr-xr-x 2 tomotoboy member 4096 Aug 6 16:30 testdirec
查找包含'Oc'的行
/home/l/g/tomotoboy >ls -al|grep 'Oc'
drwxr-xr-x 202 root root 4096 Oct 21 2008 ..
drwxr-xr-x 3 tomotoboy member 4096 Oct 20 2008 .sunstudio
-rwxr-xr-x 1 tomotoboy member 120 Oct 20 2008 greeting.sh
-rwxr-xr-x 1 tomotoboy member 116 Oct 20 2008 readme.sh
drwxr-xr-x 2 tomotoboy member 4096 Oct 21 2008 seawolf
显示满足匹配行的所有行的行数
/home/l/g/tomotoboy >ls -al|grep -n '4096'
2:drwx------ 8 tomotoboy member 4096 Aug 7 13:45 .
3:drwxr-xr-x 202 root root 4096 Oct 21 2008 ..
8:drwxr-xr-x 3 tomotoboy member 4096 Oct 20 2008 .sunstudio
9:drwxr-xr-x 2 tomotoboy member 4096 Aug 6 16:26 2
11:drwxr-xr-x 2 tomotoboy member 4096 Aug 7 19:16 c
41:drwxr-xr-x 2 tomotoboy member 4096 Jul 29 14:08 main
50:drwxr-xr-x 2 tomotoboy member 4096 Oct 21 2008 seawolf
57:drwxr-xr-x 2 tomotoboy member 4096 Aug 6 16:30 testdirec
显示不包含匹配行的行
/home/l/g/tomotoboy >ls -al|grep -v 'Aug'
total 14328
drwxr-xr-x 202 root root 4096 Oct 21 2008 ..
-rw------- 1 tomotoboy member 35 Mar 4 22:21 .lesshst
drwxr-xr-x 3 tomotoboy member 4096 Oct 20 2008 .sunstudio
-rwxr--r-- 1 tomotoboy member 68 Jul 30 16:28 change.sed
-rw-r--r-- 1 tomotoboy member 7090822 Apr 10 11:00 core.log
-rwxr-xr-x 1 tomotoboy member 7058 Mar 4 23:17 factorial
-rw-r--r-- 1 tomotoboy member 75 Mar 4 22:59 factorial.c
-rwxr-xr-x 1 tomotoboy member 120 Oct 20 2008 greeting.sh
-rw-r--r-- 1 tomotoboy member 4 Jul 29 15:17 grepgrepstrings
'.'代表一个任意字符,查看包含'M.r'的行
/home/l/g/tomotoboy >ls -al|grep -n 'M.r'
5:-rw------- 1 tomotoboy member 35 Mar 4 22:21 .lesshst
20:-rwxr-xr-x 1 tomotoboy member 7058 Mar 4 23:17 factorial
21:-rw-r--r-- 1 tomotoboy member 75 Mar 4 22:59 factorial.c
27:-rwxr-xr-x 1 tomotoboy member 9940 Mar 4 22:56 hello
28:-rw-r--r-- 1 tomotoboy member 94 Mar 4 22:17 hello.cpp
42:-rw-r--r-- 1 tomotoboy member 242 Mar 4 23:17 main.c
[size=large][b]egrep[/b][/size]
egrep代表expression或extended grep,适情况而定。egrep接受所有的正则表达式,egrep的一个显著特性是可以以一个文件作为保存的字符串,然后将之传给egrep作为参数,为此使用- f开关。