访问手机版  

Linux常用命令|Linux培训学习|考试认证|工资待遇与招聘,认准超级网工!

招聘|合作 登陆|注册

网络工程师培训

当前位置:网络工程师 > 技术课程 > linux > 热点关注 > linux常用命令

Linux常用基本命令:grep

时间:2019-09-13

linux常用shell命令_linux常用命令_linux压缩命令zip命令

作用:从文本文件或管道数据流中筛选匹配的行及数据,配合正则表达式一起使用,功能更加强大。

格式:

grep [options] [pattern] [file]

1,匹配包含"ghostwu"的行

ghostwu@dev:~/linux/grep$%20cat%20-n%20ghostwu.txt%20
%20%20%20%20%201%20%20%20%20my%20name%20is%20ghostwu
%20%20%20%20%202%20%20%20%20how%20are%20you
%20%20%20%20%203%20%20%20%20fine%20think%20you
%20%20%20%20%204%20%20%20%20My%20name%20is%20Ghostwu
%20%20%20%20%205%20%20%20%20what's%20your%20name?
%20%20%20%20%206%20%20%20%20my%20name%20is%20ghostwu2
%20%20%20%20%207%20%20%20%20
ghostwu@dev:~/linux/grep$%20grep%20"ghostwu"%20ghostwu.txt%20
my%20name%20is%20ghostwu
my%20name%20is%20ghostwu2

2,-v:%20不包含,相当于取反

ghostwu@dev:~/linux/grep$%20grep%20-v%20"ghostwu"%20ghostwu.txt%20
how%20are%20you
fine%20think%20you
My%20name%20is%20Ghostwu
what's%20your%20name?

ghostwu@dev:~/linux/grep$%20

3,-n添加行号

ghostwu@dev:~/linux/grep$ grep -n "ghostwu" ghostwu.txt 
1:my name is ghostwu
6:my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -vn "ghostwu" ghostwu.txt 
2:how are you
3:fine think you
4:My name is Ghostwu
5:what's your name?
7:

4linux常用命令,-E,使用扩展的egrep命令,模式中可以用正则表达式

ghostwu@dev:~/linux/grep$ cat ghostwu.txt 
my name is ghostwu
how are you
fine think you
My name is Ghostwu
what's your name?
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -E "my|your" ghostwu.txt 
my name is ghostwu
what's your name?
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -Ev "my|your" ghostwu.txt 
how are you
fine think you
My name is Ghostwu
ghostwu@dev:~/linux/grep$ grep -En "my|your" ghostwu.txt 
1:my name is ghostwu
5:what's your name?
6:my name is ghostwu2

5,-i选项,不区分大小写

linux常用shell命令_linux压缩命令zip命令_linux常用命令

ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt 
my name is ghostwu
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -i "ghostwu" ghostwu.txt 
my name is ghostwu
My name is Ghostwu
my name is ghostwu2