Command | grep

一、grep是什么?

grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

二、grep使用

1、参数
测试文本内容grep.txt

# cat grep.txt
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
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
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

不加参数直接使用,例如显示所有包含San的行

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

-c:只输出匹配行的总行数,例如匹配San出现的总行数

# grep -c "San" grep.txt
1

-i:不区分大小写,例如匹配steve跟Steve

# grep "steve" grep.txt
steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
# grep -i "steve" grep.txt
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300

-h:查询多文件时不显示文件名

# grep "Lori" grep.txt grep.txt
grep.txt:Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
grep.txt:Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
# grep -h "Lori" rep.txt grep.txt
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200

-l:查询多文件时只输出包含匹配字符的文件名

# grep -l "San" grep.txt test.txt
grep.txt

-n:显示匹配行及行号

# grep -n "San" grep.txt
7:Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
21:Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
25:Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600

-v:显示不包含匹配文本的所有行,例如显示除了San的其他行

# grep -v "San" grep.txt
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
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
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
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

2、使用RE(正则表达式)
^:匹配正则表达式的开始行,例如显示以J开头的行

# grep "^J" grep.txt
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

$:匹配正则表达式的结束行,例如显示以900结尾的行*

# grep "900$" grep.txt
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900

[ ]:单个字符,[A]即A符合要求,例如

# grep "[J]" grep.txt
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
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

[ - ]:范围,如[A-Z],即A一直到Z都符合要求