bash基础——grep bash功能——命令行编辑、内部命令 外部命令、命令补全 、命令历史、文件名通配符、命令别名

grep

Global RE Printing 全局搜索正则表达式,并将搜索到的行打印出来

RE Regular Expression 正则表达式:由一些元字符和其他组成的表达式。元字符是由特定意义的字符,并不代表这个字符本身。

文本过滤工具:能够根据指定的正则表达式,逐行扫面文件的内容,只要行中有满足正则表达式的内容,则整行被显示

RE

  Basic RE 基本正则表达式

  Extended RE 扩展正则表达式

--color=auto   将匹配到的关键字高亮

-v显示非关键字的那些行

[root@51cto ~]# cat uniqtest -n
     1    wang student 23
     2    wang student 23
     3    han teacher 38
     4    liu teacher 29
     5    zhang student 23
     6    wang student 23
[root@51cto ~]# grep "wang" uniqtest -v
han teacher 38
liu teacher 29
zhang student 23

-o只显示关键字

[root@51cto ~]# cat uniqtest -n
     1    wang student 23
     2    wang student 23
     3    han teacher 38
     4    liu teacher 29
     5    zhang student 23
     6    wang student 23
[root@51cto ~]# grep "wang" uniqtest -o
wang
wang
wang

-i 不区分大小写

[root@51cto ~]# cat uniqtest 
wang student 23
wang student 23
han teacher 38
liu teacher 29
zhang student 23
wang student 23
Wang student 24
[root@51cto ~]# grep "wang" uniqtest -i
wang student 23
wang student 23
wang student 23
Wang student 24

-A# 将命中的那一行下面#行也显示出来;-B#将命中的那一行上面#行也显示出来。如果前后都要显示#行,则-C#。1只是做个例子,可以是任何数字

[root@51cto ~]# cat uniqtest 
wang student 23
wang student 23
han teacher 38
liu teacher 29
zhang student 23
wang student 23
Wang student 24
[root@51cto ~]# grep "wang" uniqtest -A1
wang student 23
wang student 23
han teacher 38
--
wang student 23
Wang student 24

-E 表示后面的表达式是扩展的正则表达式

grep语法:

grep [optin]… ‘正则表达式’ file…

基本正则表达式

参考:bash功能——命令行编辑、内部命令 外部命令、命令补全 、命令历史、文件名通配符、命令别名 文件名通配符

文件名通配符

* 代表任意长度任意字符

?代表任意单个字符

[] 指定范围的任意单个字符

[^ ] 指定范围外的任意单个字符

扩展正则表达式

  正则表达式元字符

. 匹配任意单个字符

 

[] 匹配指定范围内的任意单个字符

[^ ] 匹配指定范围外的任意单个字符

任意长度任意字符

正则表达式匹配次数

* 匹配前一个字符0次~多次

? 匹配前一个字符0次或1次

\{m,n\} 匹配前一个字符最少m次 最多n次

  \{0,n\} 最多n次

  \{m\}  最少m次

猜你喜欢

转载自www.cnblogs.com/kelamoyujuzhen/p/9111973.html
今日推荐