|
正则表达式就是用于匹配每行输入的一种模式,模式是指一串字符序列。拥有强大的字符搜索功能。也非常方便的搜索过滤出我们想要的内容。 Linux系统:CentOS Linux release 8.1.1911 (Core)1、找出ifconfig “网卡名” 命令结果中本机的IPv4地址4 d. \: K! c- k5 R1 r0 O# a! vifconfig | head -n 2 |tail -1 |tr -s " " |cut -d" " -f38 p* h* R% Z* g2 y; F# `9 M
2、查出分区空间使用率的最大百分比值2 Z0 {/ u! l( G" e% E8 w df |tr -s " " |cut -d" " -f5
3、查出用户UID最大值的用户名、UID及shell类型 cat /etc/passwd | cut -d: -f1,3,7| sort -nt: -k2 |tail -n 1+ y3 h3 k7 H: \4 f: j- ^: n
4、查出/tmp的权限 stat /tmp | head -n 4 |tail -n 1|cut -c10-13
5、显示CentOS8上所有系统用户的用户名和UID# k, q4 n1 M9 V! O4 r6 U5 Q( t cat /etc/passwd |cut -d: -f1,3 | egrep -v "[0-9]{4,}"7 }+ r- Z; @; |0 M# i
6、显示三个用户root、linuxmi、mi的UID和默认shell(A8代替)/ q' _# X( _9 r" s) N/ Q: N cat /etc/passwd |egrep "^(root|A8)" |cut -d: -f1,34 f: {: i; \4 j
7、使用egrep取出/etc/rc.d/init.d/functions中显示文件中符合条件的字符" F5 j8 O3 j2 @ echo /etc/rc.d/init.d/functions | egrep "[a-z]$"5 Q y+ ^, |, W0 {; g1 {
8、使用egrep取出上面路径的目录名 echo /etc/rc.d/init.d/functions | egrep "/.*/"
9、统计last命令中以root登录的每个主机IP地址登录次数
10、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255 echo {1..255} | egrep "\<[0-9]\>") q0 S# R8 V& r& W9 q
egrep "\<1[0-9]\>"
egrep "\<1[0-9][0-9]\>"
egrep "\<2[0-4][0-9]\>"- b' r! R G9 D
egrep "\<25[0-5]\>", ?# A# v# a0 S* R" K9 s9 N. `
11、显示ifconfig命令结果中所有IPv4地址 ifconfig |egrep "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"& Q* I& d( }) P, x, Q
简单点来说,正则表达式是对一组正在处理的文本的描述。+ h* L! \) G/ } e8 r) M , r9 M+ q6 g5 C: F0 [* y |
微信公众号
手机版