1、参数:
-I :忽略大小写
-c :打印匹配的行数
-l :从多个文件中查找包含匹配项
-v :查找不包含匹配项的行
-n:打印包含匹配项的行和行标
2、RE(正则表达式)
忽略正则表达式中特殊字符的原有含义
^ 匹配正则表达式的开始行
$ 匹配正则表达式的结束行
< 从匹配正则表达式的行开始
> 到匹配正则表达式的行结束
[ ] 单个字符;如[A] 即A符合要求
[ - ] 范围 ;如[A-Z]即A,B,C一直到Z都符合要求
. 所有的单个字符
* 所有字符,长度可以为0
3、举例
# ps -ef | grep in.telnetd
root 19955 181 0 13:43:53 ? 0:00 in.telnetd
# more size.txt size文件的内容
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# more size.txt | grep [a-b] 范围 ;如[A-Z]即A,B,C一直到Z都符合要求
b124230
b034325
a081016
a022021
a061048
b103303
a013386
b044525
# more size.txt | grep [a-b] *
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# more size.txt | grep [b] 单个字符;如[A] 即A符合要求
b124230
b034325
b103303
b044525
# more size.txt | grep [bB]
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345
# grep root /etc/group
root::0:root
bin::2:root,bin,daemon
sys::3:root,bin,sys,adm
adm::4:root,adm,daemon
uucp::5:root,uucp
mail::6:root
tty::7:root,tty,adm
lp::8:root,lp,adm
nuucp::9:root,nuucp
daemon::12:root,daemon
# grep ^root /etc/group 匹配正则表达式的开始行
root::0:root
# grep uucp /etc/group
uucp::5:root,uucp
nuucp::9:root,nuucp
# grep /tmp/sharetab.$$
[ "x$fstype" != xnfs ] &&
echo "$path $res $fstype $opts $desc"
>>/tmp/sharetab.$$
/usr/bin/touch -r /etc/dfs/sharetab /tmp/sharetab.$$
/usr/bin/mv -f /tmp/sharetab.$$ /etc/dfs/sharetab
if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v ^[ ]*(#|$)
if [ $startnfsd -eq 0 -a -f /etc/rmmount.conf ] &&
if [ $startnfsd -ne 0 ]; then
elif [ ! -n "$_INIT_RUN_LEVEL" ]; then
while [ $wtime -gt 0 ]; do
wtime=`expr $wtime - 1`
if [ $wtime -eq 0 ]; then
echo "Usage: $0 { start | stop }"
# more size.txt
the test file
their are files
The end
# grep the size.txt
the test file
their are files
# grep size.txt
the test file
# grep size.txt
the test file
# grep <[Tt]he> size.txt
the test file
The end
==========================================
Linux Shell
三.grep
1.grep tiger /etc/passwd
2.grep NW d*
3.grep '^n' datafile
4.grep '4$' datafile
5.grep TB datafile1 datafile2
6.grep 'TB datafile1' datafile2
7.grep '5..' datafile
8.grep '.' datafile
9.grep '^[we]' datafile
10.grep '[^0-9]' datafile
11.grep [A-Z][A-Z][a-z]' datafile
12.grep 'ss*' datafile
13.grep '[a-z]{9}' datafile
14.grep 'north' datafile
1.egrep 'NW|EA' datafile
2.grep -E 'NW|EA' datafile
3.egrep '3+' datafile
4.grep -E '3+' datafile
5.egrep '2.?[0-9]' datafile
6.egrep '(no)+' datafile
7.grep -i 'AB' datafile
8.grep -n '^south' datafile
9.grep -v 'sourth' datafile
10.grep -version
11.grep 'San' datafile
12.grep '(CA|ca)' datafile
13.grep '^J' datafile
14.grep '700$' datafile
15.grep -i '^K' datafile
16.grep -c 'Jesse' datafile
17.grep -2 'Jesse' datafile
18.grep -A 2 'Patricaia' datafile
19.grep -B 2 'patricaia' datafile