Linux 中正则表达式如何使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
1、找出 ifconfig “网卡名” 命令结果中本机的 IPv4 地址
ifconfig | head -n 2 |tail -1 |tr -s " " |cut -d" " -f3

2、查出分区空间使用率的最大百分比值
df |tr -s " " |cut -d" " -f5

3、查出用户 UID 最大值的用户名、UID 及 shell 类型
cat /etc/passwd | cut -d: -f1,3,7| sort -nt: -k2 |tail -n 1

4、查出 /tmp 的权限
stat /tmp | head -n 4 |tail -n 1|cut -c10-13

5、显示 CentOS8 上所有系统用户的用户名和 UID
cat /etc/passwd |cut -d: -f1,3 | egrep -v "[0-9]{4,}"

6、显示三个用户 root、linuxmi、mi 的 UID 和默认 shell(A8代替)
cat /etc/passwd |egrep "^(root|A8)" |cut -d: -f1,3

7、使用 egrep 取出 /etc/rc.d/init.d/functions 中显示文件中符合条件的字符
echo /etc/rc.d/init.d/functions | egrep "[a-z]$"

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]\>"

egrep "\<1[0-9]\>"

egrep "\<1[0-9][0-9]\>"

egrep "\<2[0-4][0-9]\>"

egrep "\<25[0-5]\>"

11、显示 ifconfig 命令结果中所有 IPv4 地址
ifconfig |egrep "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注天达云行业资讯频道,感谢您对天达云的支持。