一egrep及扩展的正则表达式
正则表达式对于转义要写斜线,写起来比较麻烦
扩展的正则表达式和基本正则表达式区别在于不需要加了
egrep = grep -E
egrep [OPTIONS] PATTERN [FILE...]
扩展正则表达式的元字符:
字符匹配:
. 任意单个字符
[ ] 指定范围的字符
[^] 不在指定范围的字符
注意下面全部都少了
次数匹配:
* :匹配前面字符任意次
?: 0 或1次 次
+ :1 次或多次
{m} :匹配m次 次
{m,n} :至少m ,至多n次
位置锚定:
^ : 行首
$ : 行尾
<, : 语首
>, : 语尾
分组:
()
后向引用:1, 2, ...
或者:
a|b: a 或b
C|cat: C 或cat
(C|c)at:Cat 或cat
示例一
1 、显示/proc/meminfo 文件中以大小s 开头的行( 要求:使用两种方法)
法1:
[root@centos72 ~]# cat /proc/meminfo | grep -i "^s.*"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7796 kB
Slab: 67184 kB
SReclaimable: 29520 kB
SUnreclaim: 37664 kB
法2:
[root@centos72 ~]# cat /proc/meminfo | grep -i "^[Ss]"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7796 kB
Slab: 67184 kB
SReclaimable: 29520 kB
SUnreclaim: 37664 kB
法3:
[root@centos72 ~]# cat /proc/meminfo | grep -i "^s"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7796 kB
Slab: 67184 kB
SReclaimable: 29520 kB
SUnreclaim: 37664 kB
法4:
上面都是忽略大小写,还可以进行分组,也就意味着写出所有的可能
2 、显示/etc/passwd 文件中不以/bin/bash结尾的行
逆向思维,先显示以/bin/bash结尾的行,再取反
[root@centos72 ~]# cat /etc/passwd | grep -v "bash$"
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@centos72 ~]# cat /etc/passwd | grep -v "/bin/bash$"
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
3 、显示wang账号默认的shell 程序
法1:
-w :匹配整个单词
[root@centos72 ~]# cat /etc/passwd | grep -w "wang"
wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# cat /etc/passwd | grep -w "wang" | cut -d: -f7
/bin/bash
注意用户名是在文件的行首
法2,更严谨的写法
加上w是精确匹配
[root@centos72 ~]# cat /etc/passwd | grep -w "^wang"
wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# cat /etc/passwd | grep -w "^wang" | cut -d: -f7
/bin/bash
法3:
完全匹配,词首和词尾都锚定
[root@centos72 ~]# cat /etc/passwd | grep -w "<wang>" | cut -d: -f7
/bin/bash
法4:
脱义字符和词尾结合
[root@centos72 ~]# cat /etc/passwd | grep -w "^wang>"
wang:x:1000:1000:wang:/home/wang:/bin/bash
[root@centos72 ~]# cat /etc/passwd | grep -w "^wang>" | cut -d: -f7
/bin/bash
法5:
最简便的方法,只用一个管道
[root@centos72 ~]# grep /etc/passwd -w "^wang>" /etc/passwd | cut -d: -f7
grep: ^wang>: No such file or directory
[root@centos72 ~]# grep -w "^wang>" /etc/passwd | cut -d: -f7
/bin/bash
4 、找出/etc/passwd中的两位或三位数
法1
注意要锚定词首词尾,都是在前面的,因为ID可能是四位数,五位数,会匹配两三位数
[root@centos72 ~]# grep -o "<[0-9]{2,3}>" /etc/passwd
12
11
12
100
14
50
99
99
192
192
81
81
999
998
74
74
89
89
48
48
法2
[[:digit:]]和[0-9]等价的
写法不同,意思相同,就像中国可以写成CHINA
[root@centos72 ~]# grep -o "<[[:digit:]]{2,3}>" /etc/passwd
12
11
12
100
14
50
99
99
192
192
81
81
999
998
74
74
89
89
48
48
[root@centos72 ~]# grep -o "<[[:digit:]]{2,3}>" /etc/passwd | wc
20 20 65
[root@centos72 ~]# grep -o "<[0-9]{2,3}>" /etc/passwd | wc
20 20 65
反例,没锚定的结果
[root@centos72 ~]# echo 123456789 | grep -o "[0-9]{2,3}"
123
456
789
[root@centos72 ~]# echo 123456789 | grep "[0-9]{2,3}"
123456789
正确的写法是搜索不出来的
[root@centos72 ~]# echo 123456789 | grep -o "<[0-9]{2,3}>"
[root@centos72 ~]# echo 123456789 | grep -o "<[[:digit:]]{2,3}>"
只有两位数和三位数可以
[root@centos72 ~]# echo 12 | grep -o "<[[:digit:]]{2,3}>"
12
[root@centos72 ~]# echo 123 | grep -o "<[[:digit:]]{2,3}>"
123
5 、显示CentOS7 的/etc/grub2.cfg 文件中,至少以一个空白字符开头的且后面存非空白字符的行
法1:
使用space范围更广
[root@centos72 ~]# grep "^[[:space:]]+[^[:space:]]" /etc/grub2.cfg
load_env
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
set default="${saved_entry}"
menuentry_id_option="--id"
menuentry_id_option=""
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
if [ -z "${boot_once}" ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
set timeout_style=menu
set timeout=5
set timeout=5
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="root"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
else
search --no-floppy --fs-uuid --set=root 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
fi
linux16 /vmlinuz-3.10.0-862.el7.x86_64 root=UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 ro rhgb quiet LANG=en_US.UTF-8
initrd16 /initramfs-3.10.0-862.el7.x86_64.img
load_video
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
else
search --no-floppy --fs-uuid --set=root 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
fi
linux16 /vmlinuz-0-rescue-cb26ac281315402a9928e9a4c3bedfcd root=UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 ro rhgb quiet
initrd16 /initramfs-0-rescue-cb26ac281315402a9928e9a4c3bedfcd.img
source ${config_directory}/custom.cfg
source $prefix/custom.cfg;
法2:
[root@centos72 ~]# grep "^[[:blank:]]+[^[:blank:]]" /etc/grub2.cfg
load_env
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
set default="${saved_entry}"
menuentry_id_option="--id"
menuentry_id_option=""
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
if [ -z "${boot_once}" ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
set timeout_style=menu
set timeout=5
set timeout=5
source ${prefix}/user.cfg
if [ -n "${GRUB2_PASSWORD}" ]; then
set superusers="root"
export superusers
password_pbkdf2 root ${GRUB2_PASSWORD}
fi
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
else
search --no-floppy --fs-uuid --set=root 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
fi
linux16 /vmlinuz-3.10.0-862.el7.x86_64 root=UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 ro rhgb quiet LANG=en_US.UTF-8
initrd16 /initramfs-3.10.0-862.el7.x86_64.img
load_video
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
else
search --no-floppy --fs-uuid --set=root 92886c3f-42a3-40f4-8cf7-c6890ca3a52e
fi
linux16 /vmlinuz-0-rescue-cb26ac281315402a9928e9a4c3bedfcd root=UUID=5998ead0-b370-4859-9153-ecf4e2b9dd84 ro rhgb quiet
initrd16 /initramfs-0-rescue-cb26ac281315402a9928e9a4c3bedfcd.img
source ${config_directory}/custom.cfg
source $prefix/custom.cfg;
[root@centos72 ~]# grep "^[[:blank:]]+[^[:blank:]]" /etc/grub2.cfg | wc
64 167 2089
[root@centos72 ~]# grep "^[[:space:]]+[^[:space:]]" /etc/grub2.cfg | wc
64 167 2089
6 、找出“netstat -tan” 命令的结果中以‘LISTEN’ 后跟任意多个空白字符结尾的行
锚定的空白字符,如果后面是tab键就不能匹配了
注意空白符要考虑到tab键
[root@centos72 ~]# netstat -tan | grep "LISTEN *$"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
法1
[root@centos72 ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
法2:
[root@centos72 ~]# netstat -tan | grep "LISTEN[[:blank:]]*$"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
7 、显示CentOS7 上所有系统用户的用户名和UID
[root@centos72 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
rooter:x:1001:1001::/home/rooter:/bin/bash
先切再找,这样就可以保证切出来的UID
[root@centos72 ~]# cat /etc/passwd | cut -d: -f1,3
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
wang:1000
apache:48
rooter:1001
如果是先找,那么就把GID也显示了
[root@centos72 ~]# cat /etc/passwd | grep "<[[:digit:]]{1,3}"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
rooter:x:1001:1001::/home/rooter:/bin/bash
法1:
{1,3}表示最少一个数,最多3个数
注意要锚定词首和行尾
行首行尾是不能的,但是不能锚定词首词尾,因为如果用户名就是字符串,也会显示此用户
锚定行尾确保就是UID
[root@centos72 ~]# cut -d: -f1,3 /etc/passwd |grep "<[[:digit:]]{1,3}$"
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
apache:48
法2:
[root@centos72 ~]# cut -d: -f1,3 /etc/passwd |grep "<[0-9]{1,3}$"
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
apache:48
创建一个账号
[root@centos72 ~]# id 123
uid=1002(123) gid=1002(123) groups=1002(123)
不能锚定词首词尾,因为如果用户名就是字符串,也会显示此用户
而此用户不是系统用户
[root@centos72 ~]# cat /etc/passwd | cut -d: -f1,3 | grep "<[[:digit:]]{1,3}>"
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
apache:48
123:1002
[root@centos72 ~]# cut -d: -f1,3 /etc/passwd |grep "<[0-9]{1,3}>"
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
apache:48
123:1002
8 、 找出/etc/passwd 中用户名和shell名称一样的行
比较难
使用到了后向引用
显示从行首开始的信息
[root@centos72 ~]# grep "^(.*)" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
rooter:x:1001:1001::/home/rooter:/bin/bash
123:x:1002:1002::/home/123:/bin/bash
还是不严谨
[root@centos72 ~]# grep "^(.*):.*1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
法1:
还要锚定词首
^(.*):
使用到了行首和词尾,这样就可以取出一个单词,而且直接写出了单词的分隔符是冒号:
<1$
使用到了词首和行尾,这样就可以取出一个单词,而且单词的分隔符是/
注意在文件里面出现了2个单词的分隔符,冒号:和斜线反斜线/
[root@centos72 ~]# grep "^(.*):.*<1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
法2:
^(.*)>
使用到了行首和词尾,这样就可以取出一个单词,而且单词的分隔符是冒号:
<1$
使用到了词首和行尾,这样就可以取出一个单词,而且单词的分隔符是/
注意在文件里面出现了2个单词的分隔符,冒号:和斜线反斜线/
[root@centos72 ~]# grep "^(.*)>.*<1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
9 、利用df 和grep取出磁盘各分区利用率,并从大到小排序
执行过程
[root@centos72 ~]# df | grep /dev/sd
/dev/sda2 52403200 1134844 51268356 3% /
/dev/sda3 20961280 32944 20928336 1% /app
/dev/sda1 1038336 126596 911740 13% /boot
[root@centos72 ~]# df | grep /dev/sd | tr -s ' '
/dev/sda2 52403200 1134844 51268356 3% /
/dev/sda3 20961280 32944 20928336 1% /app
/dev/sda1 1038336 126596 911740 13% /boot
[root@centos72 ~]# df | grep /dev/sd | tr -s ' ' | cut -d" " -f5
3%
1%
13%
[root@centos72 ~]# df | grep /dev/sd | tr -s ' ' | cut -d" " -f5 | cut -d% -f1
3
1
13
最终命令
[root@centos72 ~]# df | grep /dev/sd | tr -s ' ' | cut -d" " -f5 | cut -d% -f1 | sort -rn
13
3
1
法1:
先把硬盘分区过滤出来
[root@centos72 ~]# df |grep "/dev/sd"
/dev/sda2 52403200 1188336 51214864 3% /
/dev/sda3 20961280 32980 20928300 1% /app
/dev/sda1 1038336 126596 911740 13% /boot
加上%确保数字就是加在百分号后面的。因为数字很多,要找到不同点,精确匹配
[root@centos72 ~]# df |grep "/dev/sd" |grep -o "<[[:digit:]]{1,3}%"
3%
1%
13%
最后过滤出不带%的
[root@centos72 ~]# df |grep "/dev/sd" |grep -o "<[[:digit:]]{1,3}%" |grep -o "[[:digit:]]{1,3}" |sort -nr
13
3
1
法2:
和法1差不多的
[root@centos72 ~]# df |grep "/dev/sd" |grep -o "<[0-9]{1,3}%" |grep -o "[0-9]{1,3}" |sort -nr
13
3
1
注意要考虑到其他的条件是否是一位数到三位数,比如可用空间,以免显示了不需要显示的内容
[root@centos72 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 1188336 51214864 3% /
devtmpfs 487952 0 487952 0% /dev
tmpfs 498976 0 498976 0% /dev/shm
tmpfs 498976 7796 491180 2% /run
tmpfs 498976 0 498976 0% /sys/fs/cgroup
/dev/sda3 20961280 32980 20928300 1% /app
/dev/sda1 1038336 126596 911740 13% /boot
/dev/sr0 4364408 4364408 0 100% /mnt
tmpfs 99796 0 99796 0% /run/user/0
如果分区的利用率大于几就报警
[root@centos72 ~]# df |grep "/dev/sd" |grep -o "<[0-9]{1,3}%" |grep -o "[0-9]{1,3}" |sort -nr | head -n1
13
10使用正则表达式取出IP地址
先分析IP地址的特点是最少是一位数,最多是三位数
法1:
=1900表示出第一个数字
[root@centos72 ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.137.72 netmask 255.255.255.0 broadcast 192.168.137.255
inet6 fe80::b029:2522:876f:5456 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fc:69:f8 txqueuelen 1000 (Ethernet)
RX packets 5205 bytes 451782 (441.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9182 bytes 13571409 (12.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig ens33 | grep -o "([0-9]{1,3}
后面加上点,因为有在正则表达式里面特殊含义,任意一个字符,所以要对其进行转义
ifconfig ens33 | grep -o "([0-9]{1,3}.
进行分组,并且重复3次,也就是显示了192.168.137.
注意显示了3个点
ifconfig ens33 | grep -o "([0-9]{1,3}.){3}
[root@centos72 ~]# ifconfig ens33 | grep -o "([0-9]{1,3}.){3}[0-9]{1,3}" | head -n1
192.168.137.72
192.168.137.可以归纳为xxx.xxx.xxx.
法2:使用扩展的正则表达式,去掉反斜线
[root@centos72 ~]# ifconfig ens33 |grep -Eo "([0-9]{1,3}.){3}[0-9]{1,3}" | head -n1
192.168.137.72
示例二
下面都是扩展的正则表达式
1使用egrep 取出/etc/rc.d/init.d/functions 中其基名,也就是去除路径部分的文件名functions
查看老命令72
[root@centos72 ~]# basename /etc/issue
issue
[root@centos72 ~]# basename /etc/passwd
passwd
[root@centos72 ~]# basename /etc/services
services
注意结构
[^/]+$:表示查找1 次或多次不带反斜线到行尾之间的内容
[root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "[^/]+$"
functions
假设是下面这种情况,就是基名带斜线,比如/etc/rc.d/init.d/
?: 0 或1次 ,
那么/?表示此斜线可有可无,这样两种情况都照顾到了
[root@centos72 ~]# echo "/etc/rc.d/init.d/" | egrep -o "[^/]+/?$"
init.d/
[root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "[^/]+/?$"
functions
基本正则表达式,加上斜线转义
[root@centos72 ~]# echo "/etc/rc.d/init.d/functions" | grep -o "[^/]+/?$"
functions
2使用egrep取出/etc/rc.d/init.d/functions的目录名,也就/etc/rc.d/init.d/
[root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "^/.*/"
/etc/rc.d/init.d/
^/表示行首是/,没加无法匹配
[root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "/.*/$"
不保留目录的/
先取出带目录的
[root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "^/.*/"
/etc/rc.d/init.d/
再进行过滤
[root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "^/.*/" | grep -o "^.*[^/]"
/etc/rc.d/init.d
[root@centos72 ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "^/.*/" | grep -o "^/.*[^/]"
/etc/rc.d/init.d
如果就是目录,要取上一级上面的就不适用了
[root@centos72 ~]# echo "/etc/rc.d/init.d/" |egrep -o "^/.*/"
/etc/rc.d/init.d/
3显示三个用户root、ftp、wang的UID和默认shell类型
首先要进行分组,并且用户是第1个字段,要锚定行首
[root@centos72 ~]# cat /etc/passwd |grep "(^root|^ftp|^wang)"
root:x:0:0:root:/root:/bin/bash
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
以冒号作为分割符,取出两个字段用户的UID和默认shell类型
[root@centos72 ~]# cat /etc/passwd |grep "(^root|^ftp|^wang)" | cut -d ":" -f3,7
0:/bin/bash
14:/sbin/nologin
1000:/bin/bash
4找出/etc/rc.d/init.d/functions文件中行首为某单词或者下划线后面跟一个小括号的行
注意中间也可以出现下划线
*表示匹配前面字符任意次,也就是可以出现0次到多次
[root@centos72 ~]# cat /etc/rc.d/init.d/functions |egrep "^(_|[[:alpha:]])[[:alnum:]_]*[[:space:]]*()" -o
systemctl_redirect ()
checkpid()
__kill_pids_term_kill_checkpids()
__kill_pids_term_kill()
__pids_var_run()
__pids_pidof()
daemon()
killproc()
pidfileofproc()
pidofproc()
status()
echo_success()
echo_failure()
echo_passed()
echo_warning()
update_boot_stage()
success()
failure()
passed()
warning()
action()
strstr()
is_ignored_file()
convert2sec()
is_true()
is_false()
apply_sysctl()
5、统计last命令中以root登录的每个主机IP地址登录次数
[root@centos72 ~]# last
root pts/0 192.168.137.1 Mon Jun 24 16:19 still logged in
reboot system boot 3.10.0-862.el7.x Mon Jun 24 15:40 - 12:32 (20:52)
root pts/0 192.168.137.1 Sun Jun 23 12:23 - crash (1+03:16)
reboot system boot 3.10.0-862.el7.x Sun Jun 23 12:21 - 12:32 (2+00:10)
root pts/0 192.168.137.1 Sat Jun 22 16:43 - 10:55 (18:11)
root pts/0 192.168.137.1 Sat Jun 22 16:41 - 16:42 (00:00)
root pts/1 192.168.137.1 Sat Jun 22 16:37 - 16:39 (00:01)
root pts/0 gateway Sat Jun 22 16:28 - 16:39 (00:11)
reboot system boot 3.10.0-862.el7.x Sat Jun 22 16:24 - 12:32 (2+20:07)
root pts/1 gateway Fri Jun 21 09:16 - 12:46 (1+03:30)
root pts/0 192.168.137.2 Sun Jan 13 00:48 - 11:25 (159+10:37)
root pts/0 192.168.137.2 Sun Jan 13 00:46 - 00:48 (00:02)
root tty1 Sun Jan 13 00:35 - 12:47 (160+12:12)
reboot system boot 3.10.0-862.el7.x Sun Jan 13 00:34 - 12:48 (160+12:13)
wtmp begins Sun Jan 13 00:34:55 2019
锚定行首和词尾以确定是第1个字段
[root@centos72 ~]# last | grep "^root"
root pts/0 192.168.137.1 Mon Jun 24 16:19 still logged in
root pts/0 192.168.137.1 Sun Jun 23 12:23 - crash (1+03:16)
root pts/0 192.168.137.1 Sat Jun 22 16:43 - 10:55 (18:11)
root pts/0 192.168.137.1 Sat Jun 22 16:41 - 16:42 (00:00)
root pts/1 192.168.137.1 Sat Jun 22 16:37 - 16:39 (00:01)
root pts/0 gateway Sat Jun 22 16:28 - 16:39 (00:11)
root pts/1 gateway Fri Jun 21 09:16 - 12:46 (1+03:30)
root pts/0 192.168.137.2 Sun Jan 13 00:48 - 11:25 (159+10:37)
root pts/0 192.168.137.2 Sun Jan 13 00:46 - 00:48 (00:02)
root tty1 Sun Jan 13 00:35 - 12:47 (160+12:12)
[root@centos72 ~]# last | grep "^root>"
root pts/0 192.168.137.1 Mon Jun 24 16:19 still logged in
root pts/0 192.168.137.1 Sun Jun 23 12:23 - crash (1+03:16)
root pts/0 192.168.137.1 Sat Jun 22 16:43 - 10:55 (18:11)
root pts/0 192.168.137.1 Sat Jun 22 16:41 - 16:42 (00:00)
root pts/1 192.168.137.1 Sat Jun 22 16:37 - 16:39 (00:01)
root pts/0 gateway Sat Jun 22 16:28 - 16:39 (00:11)
root pts/1 gateway Fri Jun 21 09:16 - 12:46 (1+03:30)
root pts/0 192.168.137.2 Sun Jan 13 00:48 - 11:25 (159+10:37)
root pts/0 192.168.137.2 Sun Jan 13 00:46 - 00:48 (00:02)
root tty1 Sun Jan 13 00:35 - 12:47 (160+12:12)
以空格作为分割符
[root@centos72 ~]# last | grep "^root>" | cut -d " " -f1,14
root 192.168.137.1
root 192.168.137.1
root 192.168.137.1
root 192.168.137.1
root 192.168.137.1
root gateway
root gateway
root 192.168.137.2
root 192.168.137.2
root
sort -t " " -k2表示分隔符是空格,-k表示列
按照第2列进行排序
[root@centos72 ~]# last | grep "^root>" | cut -d " " -f1,14 | sort -t " " -k2
root
root 192.168.137.1
root 192.168.137.1
root 192.168.137.1
root 192.168.137.1
root 192.168.137.1
root 192.168.137.2
root 192.168.137.2
root gateway
root gateway
-c: 显示每行重复出现的次数
[root@centos72 ~]# last | grep "^root" |cut -d " " -f1,14 |sort -t " " -k2 |uniq -c
1 root
5 root 192.168.137.1
2 root 192.168.137.2
2 root gateway
按照数字进行排序
[root@centos72 ~]# last | grep "^root" |cut -d " " -f1,14 |sort -t " " -k2 |uniq -c | sort -rn
5 root 192.168.137.1
2 root gateway
2 root 192.168.137.2
1 root
6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
0-9
[root@centos72 ~]# echo {1..1000} | egrep -o "[0-9]"
1
2
3
4
5
6
7
8
9
[root@centos72 ~]# echo {1..1000} | egrep -o "<[0-9]>"
1
2
3
4
5
6
7
8
9
[root@centos72 ~]# echo {1..1000} | egrep -o "<[0-9]>" | tr "
" " "
1 2 3 4 5 6 7 8 9 [root@centos72 ~]#
[root@centos72 ~]# echo {1..1000} | egrep -o "<[0-9]>" | tr "
" " " ;echo
1 2 3 4 5 6 7 8 9
10-99
[root@centos72 ~]# echo {1..1000} | egrep -o "[0-9]{2}" | tr "
" " " ;echo
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
[root@centos72 ~]# echo {1..1000} | egrep -o "<[0-9]{2}>" | tr "
" " " ;echo
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
100-199
[root@centos72 ~]# echo {1..1000} | egrep -o "<[0-9]{3}>" | tr "
" " " ;echo
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331