[shell] grep: a powerful text search tool

1. Basic knowledge

The grep command is a powerful text search tool that uses regular expressions to search for text and marks the matches in red.
The full name of grep is Global Regular Expression Print, which means the global regular expression version, and its usage permission is for all users.

How grep works:
it searches for a string literal in one or more files. The results of the search are sent to standard output without affecting the contents of the original file.

If you want to match multiple files, separate the file names with spaces, such as:grep "match_pattern" file_1 file_2 file_3 ...

 

Command parameters:

 -A<显示行数>:除了显示符合范本样式的那一列之外,并显示该行之后的内容。
 -B<显示行数>:除了显示符合样式的那一行之外,并显示该行之前的内容。
 -C<显示行数>:除了显示符合样式的那一行之外,并显示该行之前后的内容。
 -c:统计匹配的行数
 -e :实现多个选项间的逻辑or 关系
 -E:扩展的正则表达式
 -f FILE:从FILE获取PATTERN匹配
 -F :相当于fgrep
 -i --ignore-case #忽略字符大小写的差别。
 -n:显示匹配的行号
 -o:仅显示匹配到的字符串
 -q: 静默模式,不输出任何信息
 -s:不显示错误信息。
 -v:显示不被pattern 匹配到的行,相当于[^] 反向匹配
 -w :匹配 整个单词

 

2. Basic example

[root@localhost ~]# grep [-ivnc] '需要匹配的字符' 文件名
#-i不区分大小写
#-c统计包含匹配的行数
#-n输出行号
#-v反向匹配

1. Contains a string

#查找包含name所在行
[root@localhost ~]# grep 'name' tomAndJerry.txt
The cat's name is Tom,what's the mouse's name?
# 忽略大小写
[root@localhost ~]# grep -i 'name' tomAndJerry.txt
The cat's name is Tom,what's the mouse's name?
The mouse's Name is Jerry

2. Find the location of the string and the number of rows

#打印出含有name的行所在位置
[root@localhost ~]# grep -n 'name' tomAndJerry.txt
1:The cat's name is Tom,what's the mouse's name?

# c:包含name的行数
[root@localhost ~]# grep -c 'name' tomAndJerry.txt
1
# ci:包含name(不区分大小写)的行数
[root@localhost ~]# grep -ci 'name' tomAndJerry.txt
2

3. Reverse match

# 打印不包含name的行
[root@localhost ~]# grep -v 'name' tomAndJerry.txt
The mouse's Name is Jerry
They are good friends

# 打印不包含name(不区分大小写)的行
[root@localhost ~]# grep -vi 'name' tomAndJerry.txt
They are good friends

 
 

3. Regular examples

example text

[root@localhost ~]# cat RegExp.txt
----TEXT BEGIN----

good morning teacher
hello world is a script
gold sunshine looks beautiful
golden time flies
god bless me
what a delicious food
they teast Good
you fell glad
wrong word gooood
wrong word gl0d
wrong word gl2d
wrong word gl3d
www.helloworld.com
www@helloworld@com
Upper case
100% means pure
php have a gd module

-----TEXT END-----

1. Match the beginning and end of the line

#搜索以good开头的行
[root@localhost ~]# grep '^good' RegExp.txt
good morning teacher

match end of line

#搜索以Good结尾的行
[root@localhost ~]# grep 'Good$' RegExp.txt
they teast Good

 

2. Count the number of blank lines

#搜索空行的行数
[root@localhost ~]# grep -c '^$' RegExp.txt
2

 

3. Match multiple possibilities

#搜索包含Good和good的行
[root@localhost ~]# grep '[Gg]ood' RegExp.txt
good morning teacher
they teast Good

 

4. Invert search

#搜索一个包含ood的行,但是不能是Good或good
#尖角号表示的是“非”

[root@localhost ~]# grep '[^Gg]ood' RegExp.txt
what a delicious food
wrong word gooood

 

5. Matches a fixed number of arbitrary characters

#搜索包含一个词,该词以g开头、紧接着是两个任意字符、再接着是一个d的行
[root@localhost ~]# grep 'g..d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

#搜索包含一个词,该词以G或g开头、紧接着是两个任意字符、再接着是一个d的行
[root@localhost ~]# grep '[Gg]..d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

#搜索这样一些行,该行包含某个单词,该词满足如下条件:
#1.第一个字符可以是G或g
#2.第二个字符可以是l或o
#3.第三个字符可以是换行符之外的任意字符
#4.第四个字符一定是d
[root@localhost ~]# grep '[Gg][lo].d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

 

6. Match zero to unlimited fixed characters

#搜索这样一些行,该行包含某个单词,该词满足如下条件:
#1.以g开头
#2.g后面跟零到无限个o
#3.零到无限个o后面跟d
[root@localhost ~]# grep go*d RegExp.txt
good morning teacher
god bless me
wrong word gooood
php have a gd module

 

7. Using extended regular expressions egrep

#搜索g和d之间至少有一个o的行
#“+”代表匹配前面的字符1次以上(含1次)
[root@localhost ~]# egrep 'go+d' RegExp.txt
good morning teacher
god bless me
wrong word gooood

#搜索g和d之间只有0个或1个o的行(0次或1次)
#“?”代表匹配前面的字符1次以上
[root@localhost ~]# egrep 'go?d' RegExp.txt
god bless me
php have a gd module

#搜索有glad或gold的行
[root@localhost ~]# egrep 'glad|gold' RegExp.txt
gold sunshine looks beautiful
golden time flies
you fell glad

#搜索有glad或gold的行的另一种写法
[root@localhost ~]# egrep 'g(la|ol)d' RegExp.txt
gold sunshine looks beautiful
golden time flies
you fell glad

 
 
Reference:
"Linux System Commands and Shell Scripting Practice Guide"

Guess you like

Origin blog.csdn.net/hiliang521/article/details/131532860