正则表达式练习题及作业(8.5)

当天练习题:

基本正则表达式练习题

1.显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)   可有四种方式

[root@CentOS7 ~]# cat /proc/meminfo | grep -E "^(s|S)"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7320 kB
Slab:              74112 kB
SReclaimable:      28308 kB
SUnreclaim:        45804 kB
[root@CentOS7 ~]# cat /proc/meminfo | grep -E -i "^s"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7320 kB
Slab:              74112 kB
SReclaimable:      28308 kB
SUnreclaim:        45804 kB
[root@CentOS7 ~]# cat /proc/meminfo | grep -E "^[sS]"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7320 kB
Slab:              74112 kB
SReclaimable:      28308 kB
SUnreclaim:        45804 kB
[root@CentOS7 ~]# cat /proc/meminfo | grep -e "^s" -e "^S"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7320 kB
Slab:              74112 kB
SReclaimable:      28308 kB
SUnreclaim:        45804 kB
[root@CentOS7 ~]#

2.显示/etc/passwd文件中不以/bin/bash结尾的行

[root@CentOS6 ~]# grep -E -v "/bin/bash$" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nologin:x:510:514::/home/nologin:/sbin/nologin
[root@CentOS6 ~]#

3.显示用户rpc默认的shell程序

[root@CentOS6 ~]# grep -E "^rpc\>" /etc/passwd    #取出rpc用户信息
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
[root@CentOS6 ~]# grep -E "^rpc\>" /etc/passwd | cut -d: -f7    #取出shell
/sbin/nologin
[root@CentOS6 ~]#

4.找出/etc/passwd中的两位或三位数

[root@CentOS6 ~]# grep -o "\<[0-9]\{2,3\}\>" /etc/passwd
12
10
14
11
12
100
13
30
14
50
99
99
81
81
113
113
32
32
499
499
170
170
69
69
173
173
29
29
68
68
38
38
48
48
498
76
89
89
27
27
42
42
497
495
74
74
72
72
500
500
501
501
502
503
503
507
504
508
505
509
506
510
507
511
508
512
509
513
510
514
511
515
512
516
[root@CentOS6 ~]#

5.显示/etc/grub.conf文件中,至少以一个空白字符开头的且后面存非空白字符的行

[root@CentOS6 ~]# grep "^[[:space:]]\+[^[:space:]]" /etc/grub.conf 
    root (hd0,0)
    kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=f4d977bb-af76-4617-b07f-9c70ae42a056 rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-642.el6.x86_64.img
[root@CentOS6 ~]#

6.找出"netstat -tan"命令的结果中以'LISTEN'后跟0、1或多个空白字符结尾的行

[root@CentOS7 ~]# netstat -tan | grep -o "LISTEN[[:space:]]*$"
LISTEN     
LISTEN     
LISTEN     
LISTEN     
LISTEN     
LISTEN     
LISTEN     
[root@CentOS7 ~]#

7.添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行

[root@CentOS6 ~]# grep "^\([^:]\+\>\).*\<\1\>$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:507:511::/home/bash:/bin/bash
nologin:x:510:514::/home/nologin:/sbin/nologin
[root@CentOS6 ~]#

扩展正则表达式

1.显示当前系统root、mage或wang用户的UID和默认shell

[root@CentOS6 ~]# cat /etc/passwd | grep -E "^(root|mage|wang)\>"    #匹配出root、mage、wang三个用户信息
root:x:0:0:root:/root:/bin/bash
mage:x:511:515::/home/mage:/bin/bash
wang:x:512:516::/home/wang:/bin/bash
[root@CentOS6 ~]# cat /etc/passwd | grep -E "^(root|mage|wang)\>" | cut -d: -f1,3,7    #取出用户名、UID、shell
root:0:/bin/bash
mage:511:/bin/bash
wang:512:/bin/bash
[root@CentOS6 ~]#

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

[root@CentOS6 ~]# cat /etc/rc.d/init.d/functions | grep -E "[_[:alpha:]]+\(\)"
fstab_decode_str() {
checkpid() {
__readlink() {
__fgrep() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__umount_loop() {
__source_netdevs_fstab() {
__source_netdevs_mtab() {
__umount_loopback_loop() {
        __find_mounts() {
__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() {
action_silent() {
strstr() {
confirm() {
get_numeric_dev() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {
key_is_random() {
find_crypto_mount_point() {
init_crypto() {
[root@CentOS6 ~]#

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

[root@CentOS6 ~]# echo /etc/rc.d/init.d/functions/ | grep -E -o "[^/]+/?$"    #匹配以非斜线结尾的字符至少一次
functions/
[root@CentOS6 ~]# echo /etc/rc.d/init.d/functions/ | grep -E -o "[^/]+/?$" | grep -E -o "[^/]+"    #过滤掉结尾的/
functions
[root@CentOS6 ~]#

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

[root@CentOS6 ~]# echo /etc/rc.d/init.d/functions/ | grep -E -o "^/.*[^/]"    #首先过滤掉结尾的/
/etc/rc.d/init.d/functions
[root@CentOS6 ~]# echo /etc/rc.d/init.d/functions/ | grep -E -o "^/.*[^/]" | grep -E -o "^/.*/"    #取出目录名
/etc/rc.d/init.d/
[root@CentOS6 ~]#

5.统计以root身份登录的每个远程主机IP地址的登录次数

[root@CentOS7 ~]# last | grep -E -o "^root\>.*([[:digit:]]+\.){3}[[:digit:]]+" | tr -s ' ' | cut -d' ' -f1,3    #取出当前系统已root身份登录的IP
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 172.18.19.139
root 192.168.0.107
root 192.168.0.109
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
[root@CentOS7 ~]# last | grep -E -o "^root\>.*([[:digit:]]+\.){3}[[:digit:]]+" | tr -s ' ' | cut -d' ' -f1,3 | sort | uniq -c    #取出重复次数
     22 root 10.1.250.60
      1 root 172.18.19.139
      1 root 192.168.0.107
      1 root 192.168.0.109
[root@CentOS7 ~]#

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

[root@CentOS6 ~]# grep "[0-9] [1-9][0-9] 1[0-9]{2} 2[0-4][0-9] 25[0-5]"

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

[root@CentOS6 ~]# ifconfig | grep -E -o "(\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
10.1.252.233
10.1.255.255
255.255.0.0
127.0.0.1
255.0.0.0
[root@CentOS6 ~]#

作业练习题:

1.取本机ip地址

[root@CentOS6 ~]# ifconfig | grep -E -o "(\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
10.1.252.233
10.1.255.255
255.255.0.0
127.0.0.1
255.0.0.0
[root@CentOS6 ~]#

2.取各分区利用率的数值

[root@CentOS6 ~]# df | tr -s ' ' | cut -d' ' -f5
Use%
10%
1%
19%
1%
[root@CentOS6 ~]#

3.统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

[root@CentOS6 ~]# cat /etc/rc.d/init.d/functions | tr -sc '[:alpha:]' '\n' | sort | uniq -c | sort -nr -t' ' -k1
     83 if
     77 then
     75 pid
     73 echo
     72 fi
     61 return
     57 dev
     54 file
     50 n
     46 local
     42 kill
     39 z
     36 base
     35 remaining
     31 a
     30 d
     27 in
     25 null
     24 key
     23 is
     23 fstab
     23 done
     23 do
     23 awk
     22 pids
     22 list
     21 for
     21 BOOTUP
     20 to
     20 p
     20 dst
     19 shift
    ...

4.正则表达式表示身份证号

[root@CentOS7 ~]# egrep "\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|([0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>" number.txt 
210905197807210546 
370205197405213513 
372922198012224773 
370722197812222517
230803197906010035 
152801198703025310 
511428196305026357 
[root@CentOS7 ~]#

    

5.正则表达式表示手机号

[root@CentOS7 ~]# grep -E -o "(\+86)?1[38][0-9]{9}|14[57][0-9]{8}|15[0-35-9][0-9]{8}|17[0678][0-9]{8}" iphone.txt 
+8613868233891
15003107238
[root@CentOS7 ~]#

6.正则表达式表示邮箱

[root@CentOS7 ~]# egrep "\<([[:alnum:]]+(-|_)*[[:alnum:]]*)\>@([[:alnum:]]+\.)+[[:alnum:]]+" mail.txt 
993667135@qq.com
servername@163.com
[root@CentOS7 ~]#

7.正则表达式表示QQ号

[root@CentOS7 ~]# grep -E -o '\b[1-9][0-9]{4,12}\b' qq.txt 
333449
521521
796898
765423
93796117258
[root@CentOS7 ~]#

原创文章,作者:zhai796898,如若转载,请注明出处:http://www.178linux.com/30996

(2)
zhai796898zhai796898
上一篇 2016-08-15 14:31
下一篇 2016-08-15 14:31

相关推荐

  • systemd

    systemd的新特性(centos 7) 系统引导时实现服务并行启动 按需激活进程,在此之前可以让进程处于半活动状态 系统状态快照:能够自我保存当前系统上没一个用户空间的进程运行状态快照,将来可以迅速恢复到某一种状态,因此用户空间有了时间机器。可以回滚到过去某一时刻的状态 内在的基于依赖关系定义的服务控制逻辑 对与systemd来讲,引入了一个新的核心概念…

    Linux干货 2016-09-23
  • Linux第四周总结

    1、复制/etc/skel目录为/home/tuser1, 要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。 3、手动编辑/etc/passwd文件新增一行,添加用户hadoop, 其基本组ID为hadoop组的id号;其家目录为/home/hadoop。 4、复制/etc/…

    2017-07-24
  • N26-博客作业-week6

    vim:Vi IMprove 基本模式:编辑模式,命令模式输入模式末行模式 打开文件: vim [option] [file…] +#:打开文件后,直接让光标处于第#行的行首+/PATTERN:打开文件后,直接让光标处于第一个被PATTERN匹配到的行的行首 模式转换:编辑模式:默认模式编辑模式 –> 输入模式:i:insert…

    Linux干货 2017-03-12
  • Linux中的包管理

    1. RPM介绍 rpm是linux中的包管理软件,通过rpm用户可以对rpm包进行查询、安装、卸载、升级和校验等操作。 1.1 查询 查询 -q:可以查看某个包是否已经安装 -qa:查看系统已经安装的所有包 -qi:查询某个安装包的详细情况 -ql:查询安装某个包之后会生成哪些文件 -qc:查询某个软件的配置文件 -qd:查询某个软件的所有文档 &#821…

    Linux干货 2017-04-17
  • 文本处理三剑客-sed

    1.文本处理三剑客-sed #sed语法: sed  [OPTION]…  {script-only-if-no-other-script}        [input-file]… sed理解:是一种流编辑器,一次处理一行内容,处理时把当前…

    Linux干货 2016-08-12
  • Linux-raid的工作原理与管理

    本章内容:        1、RAID详细描述        2、mdadm 工具        3、创建raid        4、磁盘阵列的管理 RAID描述:     &nb…

    2017-06-24