正则表达式就是用于匹配每行输入的一种模式,模式是指一串字符序列。拥有强大的字符搜索功能。也非常方便的搜索过滤出我们想要的内容。 Linux系统:
CentOS Linux release 8.1.1911 (Core)1、找出 ifconfig “网卡名” 命令结果中本机的 IPv4 地址ifconfig | head -n 2 |tail -1 |tr -s ' ' |cut -d' ' -f3
df |tr -s ' ' |cut -d' ' -f5 3、查出用户 UID 最大值的用户名、UID 及 shell 类型cat /etc/passwd | cut -d: -f1,3,7| sort -nt: -k2 |tail -n 1
stat /tmp | head -n 4 |tail -n 1|cut -c10-13
5、显示 CentOS8 上所有系统用户的用户名和 UIDcat /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]$'
echo /etc/rc.d/init.d/functions | egrep '/.*/'
9、统计 last 命令中以 root 登录的每个主机IP地址登录次数10、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255echo {1..255} | egrep '\<[0-9]\>'
11、显示 ifconfig 命令结果中所有 IPv4 地址ifconfig |egrep '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
简单点来说,正则表达式是对一组正在处理的文本的描述。 官方站点:www.linuxprobe.com Linux命令大全:www.linuxcool.com
|