扩展正则表达式练习

1、显示三个用户root、mage、wang的UID和默认shell

[10:00:24 root@sh-pd-crm-sit-102 ~]# cat /etc/passwd|egrep "^(root|mage|wang)\>"|cut -d: -f1,3,7
root:0:/bin/bash
mage:1001:/bin/bash
wang:1002:/bin/bash
[10:00:52 root@sh-pd-crm-sit-102 ~]# 

 2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[13:12:00 root@sh-pd-crm-sit-102 ~]# cat /etc/rc.d/init.d/functions | egrep -o "^([[:alpha:]]|_)+\(\)"
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()
is_true()
is_false()
apply_sysctl()
[13:12:05 root@sh-pd-crm-sit-102 ~]# 
[13:15:09 root@sh-pd-crm-sit-102 ~]# cat /etc/rc.d/init.d/functions | egrep -o "^[[:alpha:]_]+\(\)"

3、使用egrep取出/etc/rc.d/init.d/functions中其基名

[13:44:57 root@sh-pd-crm-sit-102 ~]# echo /etc/rc.d/init.d/functions |grep -Eo "[[:alpha:]]+$"
functions
[13:45:50 root@sh-pd-crm-sit-102 ~]# echo /etc/rc.d/init.d/functions |grep -Eo "[^/]+$"
functions
[13:46:25 root@sh-pd-crm-sit-102 ~]# echo /etc/rc.d/init.d/functions |grep -Eo "[^/]+/?$"
functions
[13:46:44 root@sh-pd-crm-sit-102 ~]# echo /etc/rc.d/init.d/ |grep -Eo "[^/]+/?$"
init.d/
[13:46:53 root@sh-pd-crm-sit-102 ~]# 

4、使用egrep取出上面路径的目录名

[13:49:10 root@sh-pd-crm-sit-102 ~]# echo /etc/rc.d/init.d/functions |egrep -o "^/.*/"
/etc/rc.d/init.d/
[10:40:39 root@sh-pd-crm-sit-102 ~]# echo /etc/rc.d/init.d/functions |egrep -o "^/.*/[[:alnum:]]"|egrep -o ".*/"
/etc/rc.d/init.d/

5、统计last命令中以root登录的每个主机IP地址登录次数

[10:46:51 root@sh-pd-crm-sit-102 ~]# last|egrep "\<root\>"|tr -s ' '|cut -d" " -f3|egrep "([0-9]{1,3}.){3}[0-9]{1,3}"|sort|uniq -c
     10 172.16.7.110
      3 172.16.7.174
     79 172.16.7.229
[10:46:54 root@sh-pd-crm-sit-102 ~]# 

6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

[10:51:16 root@sh-pd-crm-sit-102 ~]# seq 255 > test.txt
[10:56:48 root@sh-pd-crm-sit-102 ~]# egrep "\<[0-9]\>" test.txt 

[10:56:48 root@sh-pd-crm-sit-102 ~]# egrep "\<[1-9][0-9]\>" test.txt 

[10:56:48 root@sh-pd-crm-sit-102 ~]# egrep "\<[1][0-9][0-9]\>" test.txt 

[10:56:48 root@sh-pd-crm-sit-102 ~]# egrep "\<[2][0-4][0-9]\>" test.txt 

[10:56:38 root@sh-pd-crm-sit-102 ~]# egrep "\<[2][5][0-5]\>" test.txt

7、显示ifconfig命令结果中所有IPv4地址

[11:07:08 root@sh-pd-crm-sit-102 ~]# ifconfig|egrep -o "([0-255]\.){3}[0-255]"
0.0.0.1
0.0.0.2
5.0.0.0
# 这个结果是错误的,[0-255] 的意思 是  0,1,2或者5,这也是一个小彩蛋了。哈哈
[11:07:29 root@sh-pd-crm-sit-102 ~]# ifconfig|egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"
10.0.0.102
255.255.255.0
10.0.0.255
127.0.0.1
255.0.0.0
[11:07:56 root@sh-pd-crm-sit-102 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.102  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::eb1b:1357:b362:2292  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:6b:4b:bc  txqueuelen 1000  (Ethernet)
        RX packets 115962  bytes 50890748 (48.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 59984  bytes 6236156 (5.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64  bytes 5568 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5568 (5.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面

[11:16:29 root@sh-pd-crm-sit-102 ~]# echo welcome to magedu linux |egrep -o [a-zA-Z]|sort|uniq -c|sort -nr
      3 e
      2 u
      2 o
      2 m
      2 l
      1 x
      1 w
      1 t
      1 n
      1 i
      1 g
      1 d
      1 c
      1 a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值