Linux入门(Ubuntu)之文本三剑客(grep)与正则表达式

grep命令

知识简介
  • grep 命令用于查找内容包含指定模式的文件,如果发现某文件的内容符合所指定的模式样式,预设 grep 指令会把含有模式样式的那一列显示出来。
  • grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的扩展,支持更多的re元字符,fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义。
  • 在关键字的显示方面,grep 可以使用 --color=auto 来将关键字部分使用颜色显示。
常用参数
  • -e:开启正则表达式
  • -i :忽略大小写
  • -v:反过来(invert),只打印没有匹配的,而匹配的反而不打印
  • -n:显示行号
  • -w:被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker
  • -c:显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到
  • -o:只显示被模式匹配到的字符串
  • -A #:显示匹配到的字符串所在的行及其后n行,after
  • -B #:显示匹配到的字符串所在的行及其前n行,before
  • -C #:显示匹配到的字符串所在的行及其前后各n行,context
    示例:
    -n 显示行号
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep -n /bin
1:root:x:0:0:root:/root:/bin/bash
3:bin:x:2:2:bin:/bin:/usr/sbin/nologin
5:sync:x:4:65534:sync:/bin:/bin/sync
12:proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
30:speech-dispatcher:x:111:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false

-c 统计匹配行数

liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep -c /bin
10

-o 显示匹配内容

liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep -o /bin
/bin
/bin
/bin
模式部分(正则表达式)

相关简介
正则表达式由一类特殊的字符 或文本组成,有些字符(元字符)并不表示其字面内容,而是表示控制或通配功能。

字符通配

  • . 匹配任意单个字符
  • [ ] 匹配指定范围内的任意单个字符
  • [^] 匹配指定范围外的任意单个字符
  • [:alnum:] 字母和数字
  • [:alpha:] 代表任何英文大小写字符,亦即A-Z, a-z
  • [:lower:] 小写字母[:upper:] 大写字母
  • [:blank:] 空白字符(空格和制表符)
  • [:space:]水平和垂直的空白字符(比[:blank:]包含的范围广)
  • [:cntrl:] 不可打印的控制字符(退格、删除、警铃…)
  • [:digit:] 十进制数字[:xdigit:]十六进制数字
  • [:graph:] 可打印的非空白字符
  • [:print:] 可打印字符
  • [:punct:] 标点符号
    示例:
    . 匹配任意单个字符
liuyan@liuyan-virtual-machine:~$ netstat -ant|grep -o 192.1.
192.16
192.16
192.16

[:alnum:] 字母和数字

liuyan@liuyan-virtual-machine:~$ netstat -ant|grep -o [[:alnum:]]
A
c
1

[:lower:] 小写字母[:upper:] 大写字母

liuyan@liuyan-virtual-machine:~$ netstat -ant|grep -o [[:lower:]]
c
t
i

[:digit:] 十进制数字[:xdigit:]十六进制数字

liuyan@liuyan-virtual-machine:~$ netstat -ant|grep -o [[:digit:]]
0
0
1
2
7

综合示例:

liuyan@liuyan-virtual-machine:~$ netstat -ant|grep [[:lower:]][[:lower:]][[:alnum:]][[:digit:]]
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN   

次数通配

  • *---------匹配前面的字符任意次,包括0次(贪婪模式:尽可能长的匹配)
  • .*--------任意长度的任意字符
  • ?---------匹配其前面的字符0或1次
  • ±---------匹配其前面的字符至少1次
  • {n}-------匹配前面的字符n次
  • {m,n}----匹配前面的字符至少m次,至多n次
  • {,n}------匹配前面的字符至多n次
  • {n,}------匹配前面的字符至少n次
    示例:
    {n}-------匹配前面的字符n次
liuyan@liuyan-virtual-machine:~/Desktop/Text/practice$ cat ff
telephonenumber is 13356487956
liuyan@liuyan-virtual-machine:~/Desktop/Text/practice$ cat ff|grep -o 1[[:digit:]]\\{10\\}
18695250179

{m,n}----匹配前面的字符至少m次,至多n次

liuyan@liuyan-virtual-machine:~$ netstat -ant|grep -o [[:digit:]]\\{1\,3\\}\\.[[:digit:]]\\{1\,3\\}\\.[[:digit:]]\\{1\,3\\}\\.[[:digit:]]\\{1\,3\\}
127.0.0.53
0.0.0.0
0.0.0.0
0.0.0.0
127.0.0.1
0.0.0.0

位置锚定

扫描二维码关注公众号,回复: 9658169 查看本文章
  • ^ 行首锚定,用于模式的最左侧
  • $ 行尾锚定,用于模式的最右侧
  • ^PATTERN$ 用于模式匹配整行
  • ^$ 空行
  • ^[[:space:]]*$ 空白行
  • < 或\b词首锚定,用于单词模式的左侧
  • > 或\b词尾锚定;用于单词模式的右侧
  • <PATTERN>匹配整个单词
    示例:
    ^ 行首锚定,用于模式的最左侧
liuyan@liuyan-virtual-machine:~/Desktop/Text/practice$ cat ff|grep 1[[:digit:]]\\{10\\}
telephonenumber is 13356487956
liuyan@liuyan-virtual-machine:~/Desktop/Text/practice$ cat ff|grep -o ^tel[[:lower:]]\\{1\,\\}
telephonenumber

$ 行尾锚定,用于模式的最右侧

liuyan@liuyan-virtual-machine:~/Desktop/Text/practice$ cat ff|grep 1[[:digit:]]\\{10\\}
telephonenumber is 13356487956
liuyan@liuyan-virtual-machine:~/Desktop/Text/practice$ cat ff|grep -o [[:digit:]]\\{1\,\\}$
13356487956

综合案例

- 显示某目录下某文件中以大小s开头的行

  • 首先输入cat命令查看相关信息
liuyan@liuyan-virtual-machine:~$ cat /proc/meminfo 
MemTotal:        4002280 kB
MemFree:          527556 kB
MemAvailable:    1396708 kB
Buffers:           68360 kB
Cached:          1044216 kB
SwapCached:            0 kB
Active:          2171800 kB
  • 然后利用管道+grep命令过滤主要信息
liuyan@liuyan-virtual-machine:~$ cat /proc/meminfo |grep -e ^S -e ^s
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             79768 kB
Slab:       

- 显示/etc/passwd文件中不以/bin/bash结尾的行

  • 首先输入cat命令查看目标信息
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  • 然后通过管道利用grep命令过滤以/bin/bash结尾的信息
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep /bin/bash$
root:x:0:0:root:/root:/bin/bash
liuyan:x:1000:1000:liuyan,,,:/home/liuyan:/bin/bash
  • 最后加入-v命令逆置过滤结果即可
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep -v /bin/bash$
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin

- 找出/etc/passwd中的两位或三位数

  • 首先输入cat命令通过管道利用grep命令过滤含有两三位数的信息
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep [[:digit:]]\\{2\,3\\}
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
  • 然后通过-o命令将数字提取即可
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep -o [[:digit:]]\\{2\,3\\}
655
34
60
12
10

- 显示Ubuntu上所有系统用户的用户名和UID

  • 首先输入cat命令查看信息
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
  • 然后铜鼓管道加grep命令筛选用户名
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep ^[[:lower:]]\\{1\,\\}:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin

  • 利用正则表达式筛选用户名和Uid,最后利用-o命令提取即可
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep -o -e ^[[:lower:]]\\{1\,\\}: -e :[[:digit:]]\\{1\,\\}:
root:
:0:
daemon:
:1:
bin:
:2:
sys:

- 找出/etc/passwd用户名同shell名的行

  • 首先利用cat命令查看所属信息,然后通过管道利用grep命令过滤出用户名
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep -o -e ^[[:lower:]]\\{1\,\\}
root
daemon
bin
sys
sync
games
man
lp
  • 然后利用正则表达式过滤出bash,再利用-o命令提取即可
liuyan@liuyan-virtual-machine:~$ cat /etc/passwd|grep -o -e ^[[:lower:]]\\{1\,\\} -e /[[:lower:]]\\{1\,\\}$
root
/bash
daemon
/nologin
bin
/nologin
sys
/nologin
  • 最后利用tr命令压缩空格,并赋值给数组后输出
liuyan@liuyan-virtual-machine:~$ a=$(cat /etc/passwd|grep -o -e ^[[:lower:]]\\{1\,\\} -e /[[:lower:]]\\{1\,\\}$|tr -s " ")
liuyan@liuyan-virtual-machine:~$ echo ${a}
root /bash daemon /nologin bin /nologin sys /nologin sync /sync games /nologin man /nologin lp /nologin mail /nologin news /nologin uucp /nologin proxy /nologin

- 利用df和grep,取出磁盘各分区利用率,并从大到小排序

  • 利用df命令查看硬件使用数据
liuyan@liuyan-virtual-machine:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           391M  2.0M  389M   1% /run
/dev/sda1        49G  9.3G   38G  20% /
tmpfs           2.0G   89M  1.9G   5% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
  • 然后通过管道过滤百分比数字,再利用sort命令排序即可
liuyan@liuyan-virtual-machine:~$ df -h|grep [[:digit:]]\\{1\,3\\}%|sort -n
/dev/loop0       35M   35M     0 100% /snap/gtk-common-themes/818
/dev/loop10      92M   92M     0 100% /snap/core/8689
/dev/loop11      13M   13M     0 100% /snap/gnome-characters/139
/dev/loop1      2.3M  2.3M     0 100% /snap/gnome-calculator/260
/dev/loop12     4.3M  4.3M     0 100% /snap/gnome-calculator/544
发布了15 篇原创文章 · 获赞 18 · 访问量 738

猜你喜欢

转载自blog.csdn.net/qq_42452450/article/details/104596013