Linux中正则表达式的练习集合

1、找出ifconfig “网卡名” 命令结果中本机的IPv4地址
ifconfig | head -n 2 |tail -1 |tr -s " " |cut -d" " -f3
Linux中正则表达式的练习集合
2、查出分区空间使用率的最大百分比值
df |tr -s " " |cut -d" " -f5
Linux中正则表达式的练习集合
3、查出用户UID最大值的用户名、UID及shell类型
cat /etc/passwd | cut -d: -f1,3,7| sort -nt: -k2 |tail -n 1
Linux中正则表达式的练习集合
4、查出/tmp的权限,以数字方式显示
stat /tmp | head -n 4 |tail -n 1|cut -c10-13
Linux中正则表达式的练习集合
5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序(无图)
cat /var/log/httpd/access_log |cut -d" " -f1 |sort |uniq -c |sort -n -r |head
6、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行
grep "^[[:blank:]]+" /etc/grub2.cfg |grep -v "^[[:space:]]$"
Linux中正则表达式的练习集合
7、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行
netstat -tan |grep "LISTEN[[:blank:]]
$"
Linux中正则表达式的练习集合
8、显示CentOS7上所有系统用户的用户名和UID
cat /etc/passwd |cut -d: -f1,3 | egrep -v "[0-9]{4,}"
Linux中正则表达式的练习集合
9、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名同shell名的行
grep "(^.)\>.\<\1$" /etc/passwd
Linux中正则表达式的练习集合
10、利用df和grep,取出磁盘各分区利用率,并从大到小排序
df |egrep "\<[0-9]%." -o|sort -nr
Linux中正则表达式的练习集合
11、显示三个用户root、mage、wang的UID和默认shell(C7代替)
cat /etc/passwd |egrep "^(root|C7)" |cut -d: -f1,3
Linux中正则表达式的练习集合
12、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
egrep ".()" /etc/rc.d/init.d/functions
Linux中正则表达式的练习集合
13、使用egrep取出/etc/rc.d/init.d/functions中其基名(弄得有点投机嫌疑)
echo /etc/rc.d/init.d/functions | egrep "[a-z]
$"
Linux中正则表达式的练习集合
14、使用egrep取出上面路径的目录名
echo /etc/rc.d/init.d/functions | egrep "/.*/"
Linux中正则表达式的练习集合
15、统计last命令中以root登录的每个主机IP地址登录次数
last|cut -c23-38 |sort|uniq -c|sort -rn|head -n 1
Linux中正则表达式的练习集合
16、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
echo {1..255} |
egrep "\<[0-9]\>"
Linux中正则表达式的练习集合
egrep "\<1[0-9]\>"
Linux中正则表达式的练习集合
egrep "\<1[0-9][0-9]\>"
Linux中正则表达式的练习集合
egrep "\<2[0-4][0-9]\>"
Linux中正则表达式的练习集合
egrep "\<25[0-5]\>"
Linux中正则表达式的练习集合
17、显示ifconfig命令结果中所有IPv4地址(瞎写)
ifconfig |egrep "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
Linux中正则表达式的练习集合
18、将此字符串:welcome to tiantianpaokuzhentmdehaowan linux 中的每个字符去重并排序,重复次数多的排到前面
echo wecomele to tiantianpaokuzhentmdehaowan linux |grep -o . |sort |uniq -c |sort -nr
Linux中正则表达式的练习集合
如有错误之处还请批评指正。

猜你喜欢

转载自blog.51cto.com/13116366/2120839