linux三剑客之:grep

admin 2021年04月07日 1,791次浏览

1、正则表达式概述

正则表达式就是处理字符串的方法,它以行为单位来进行字符串的处理操作,正则表达式通过一些特殊字符的辅助,可以让用户轻易的完成查找、删除、替换某些特定字符串的处理过程

2、常用特殊符号

特殊字符含义
^word指定字符开头
word$指定字符结尾
.一个任意字符
*零到多个*前面的字符
[list]包含集合里面的任意字符
[n1 - n2]包含n1到n2之间任意字符
[^list]不包含集合中的字符
\{n,m\}连续 n 到 m 个前面的字符
[:alnum:]大小写字符及数字,即 0-9、 A-Z、a-z
[:alpha:]大小写字母,即 A-Z、 a-z
[:blank:]空格键与【TAB】键
[:cntrl:]键盘上的控制按键,包括 CR、LF、Tab、Del等
[:digit:]数字,即 0 - 9
[:graph:]除了空格符(空格键与【TAB】键)以外的所有按键
[:lower:]所有小写字母,即 a-z
[:print:]任何可以被打印出来的字符
[:punct:]标点符号,即:" ' ? ! ; : # $等
[:upper:]大写字母,即 A-Z
[:space:]空白字符,包括空白鍵、[Tab]、 CR 等
[:xdigit:]十六进制字符,包括: 0-9、A-F、 a-f

3、grep

  • 作用

    根据模式搜索文本,并将包含关键字的行显示出来

  • 语法

    grep [选项] 文件

  • 常用选项

    -a:将二进制文件以文本的方式查找数据
    -c:统计 '查找字符' 的数量
    -E:扩展正则表达式
    -i:忽略大小写
    -n:显示行号
    -v:显示不包含关键字的行
    --color=auto:加亮显示

  • 示例用文本

    1	"Open Source" is a good mechanism to develop programs.
    2	apple is my favorite food.
    3	Football game is not use feet only.
    4	this dress doesn't fit me.
    5	However, this dress is about $ 3183 dollars.^M
    6	GNU is free air not free beer.^M
    7	Her hair is very beauty.^M
    8	I can't finish the test.^M
    9	Oh! The soup taste good.^M
    10	motorcycle is cheap than car.
    11	This window is clear.
    12	the symbol '*' is represented as start.
    13	Oh!^IMy god!
    14	The gd software is a library for drafting programs.^M
    15	You are the best is mean you are the no. 1.
    16	The world <Happy> is the same with "glad".
    17	I like dog.
    18	google is the best tools for search keyword.
    19	goooooogle yes!
    20	go! go! Let's go.
    21	# I am VBird
    22	
    
  • 使用示例

    # 查找特定字符
    [root@localhost ~]# grep -n 'the' test.txt 
    8:I can't finish the test.
    12:the symbol '*' is represented as start.
    15:You are the best is mean you are the no. 1.
    16:The world <Happy> is the same with "glad".
    18:google is the best tools for search keyword.
    
    # 查找不包含特定字符
    [root@localhost ~]# grep -nv 'the' test.txt
    
    # 查找包含[]中任意字符
    ## 在[]中只需要匹配到任意一个即可
    [root@localhost ~]# grep -n 't[ae]st' test.txt 
    8:I can't finish the test.
    9:Oh! The soup taste good.
    
    # 排除以特定符号开头的字符
    [root@localhost ~]# grep -n '[^t]oo' test.txt 
    1:"Open Source" is a good mechanism to develop programs.
    2:apple is my favorite food.
    3:Football game is not use feet only.
    9:Oh! The soup taste good.
    18:google is the best tools for search keyword.
    19:goooooogle yes!
    
    # 查找包含数字的行
    [root@localhost ~]# grep -n [0-9] test.txt
    [root@localhost ~]# grep -n [[:digit:]] test.txt 
    5:However, this dress is about $ 3183 dollars.
    15:You are the best is mean you are the no. 1.
    
    # 查找特定字符开头的行(小写字母开头)
    [root@localhost ~]# grep -n ^[a-z] test.txt 
    [root@localhost ~]# grep -n ^[[:lower:]] test.txt 
    2:apple is my favorite food.
    4:this dress doesn't fit me.
    10:motorcycle is cheap than car.
    12:the symbol '*' is represented as start.
    18:google is the best tools for search keyword.
    19:goooooogle yes!
    20:go! go! Let's go.
    
    # 查找特定字符结尾的行
    [root@localhost ~]# grep -n '\.$' test.txt 
    1:"Open Source" is a good mechanism to develop programs.
    2:apple is my favorite food.
    3:Football game is not use feet only.
    4:this dress doesn't fit me.
    10:motorcycle is cheap than car.
    11:This window is clear.
    12:the symbol '*' is represented as start.
    15:You are the best is mean you are the no. 1.
    16:The world <Happy> is the same with "glad".
    17:I like dog.
    18:google is the best tools for search keyword.
    20:go! go! Let's go.
    
    # 查找空白行
    [root@localhost ~]# grep -n '^$' test.txt 
    22:
    
    # 使用“.”查找指定长度并包含指定开头与结尾的字符
    [root@localhost ~]# grep -n 'g..d' test.txt 
    1:"Open Source" is a good mechanism to develop programs.
    9:Oh! The soup taste good.
    16:The world <Happy> is the same with "glad".
    
    # 使用“*”查找包含任意多个*前面字符的行
    ## w*:表示包含零个到任意多个w
    [root@localhost ~]# grep -n 'go*d' test.txt 
    1:"Open Source" is a good mechanism to develop programs.
    9:Oh! The soup taste good.
    13:Oh!	My god!
    14:The gd software is a library for drafting programs.
    
    # 使用{}查找g与g之间包含指定个数o的字符
    [root@localhost ~]# grep -n 'go\{2,5\}g' test.txt 
    18:google is the best tools for search keyword.
    
    [root@localhost ~]# grep -n 'go\{2,\}g' test.txt 
    18:google is the best tools for search keyword.
    19:goooooogle yes!