正则表达式练习

1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)

[11:51:57 root@sh-pd-crm-sit-102 ~]# cat /proc/meminfo | grep "^[Ss]"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             34464 kB
Slab:              94572 kB
SReclaimable:      52500 kB
SUnreclaim:        42072 kB
[12:20:55 root@sh-pd-crm-sit-102 ~]# grep -i "^s" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             34464 kB
Slab:              94616 kB
SReclaimable:      52504 kB
SUnreclaim:        42112 kB
[11:54:48 root@sh-pd-crm-sit-102 ~]# cat /proc/meminfo | grep "^\(S\|s\)"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             34464 kB
Slab:              94572 kB
SReclaimable:      52500 kB
SUnreclaim:        42072 kB
[12:22:24 root@sh-pd-crm-sit-102 ~]# grep -e ^s -e ^S  /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             34464 kB
Slab:              94616 kB
SReclaimable:      52504 kB
SUnreclaim:        42112 kB

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

[11:56:14 root@sh-pd-crm-sit-102 ~]# cat /etc/passwd | grep -v /bin/bash$
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin

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

[12:25:17 root@sh-pd-crm-sit-102 ~]# cat /etc/passwd | grep "^rpc\>"|cut -d: -f7
/bin/bash
[12:25:26 root@sh-pd-crm-sit-102 ~]# cat /etc/passwd | grep "^rpc\>"| grep -o "[[:alpha:]/]*$"
/bin/bash
[12:26:00 root@sh-pd-crm-sit-102 ~]# 


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

[12:12:17 root@sh-pd-crm-sit-102 ~]# cat /etc/passwd | grep -o  "\<[[:digit:]]\{2,3\}\>"
12
11
12
100
14
50
99
99
192
192
81
81
999
998
74
74
89
89
998
996
[12:12:27 root@sh-pd-crm-sit-102 ~]# 

5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行

[12:17:30 root@sh-pd-crm-sit-102 ~]# cat /etc/grub2.cfg | grep "^[[:space:]]\+[^[:space:]]"
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
  set timeout_style=menu
  set timeout=5
  set timeout=5
  source ${prefix}/user.cfg
  if [ -n "${GRUB2_PASSWORD}" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 root ${GRUB2_PASSWORD}
  fi
	load_video
	set gfxpayload=keep
	insmod gzio
	insmod part_msdos
	insmod xfs
	set root='hd0,msdos1'
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  84159bbb-4882-49dc-a811-e9a3bd0f00b5
	else
	  search --no-floppy --fs-uuid --set=root 84159bbb-4882-49dc-a811-e9a3bd0f00b5
	fi
	linux16 /vmlinuz-3.10.0-1160.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8
	initrd16 /initramfs-3.10.0-1160.el7.x86_64.img
	load_video
	insmod gzio
	insmod part_msdos
	insmod xfs
	set root='hd0,msdos1'
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  84159bbb-4882-49dc-a811-e9a3bd0f00b5
	else
	  search --no-floppy --fs-uuid --set=root 84159bbb-4882-49dc-a811-e9a3bd0f00b5
	fi
	linux16 /vmlinuz-0-rescue-156872de010846f386b54d49b3457d51 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet
	initrd16 /initramfs-0-rescue-156872de010846f386b54d49b3457d51.img
  source ${config_directory}/custom.cfg
  source $prefix/custom.cfg;
[12:17:45 root@sh-pd-crm-sit-102 ~]# 

6、找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行

[12:26:41 root@sh-pd-crm-sit-102 ~]# netstat -tan | grep "\<LISTEN[[:space:]]*"
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
[12:28:09 root@sh-pd-crm-sit-102 ~]# 

7、显示CentOS7上所有UID小于1000以内的用户名和UID

[12:33:00 root@sh-pd-crm-sit-102 ~]# cat /etc/passwd | cut -d: -f1,3|grep  "\<[[:digit:]]\{1,3\}\>"
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
chrony:998
[12:33:04 root@sh-pd-crm-sit-102 ~]# 

8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户
名和shell同名的行

[12:51:27 root@sh-pd-crm-sit-102 ~]# cat /etc/passwd | grep "\(^[[:alnum:]]*\>\).*/\1$"
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:1004:1004::/home/bash:/bin/bash
nologin:x:1008:1008::/home/nologin:/sbin/nologin
[12:53:06 root@sh-pd-crm-sit-102 ~]# 

9、利用df和grep,取出磁盘各分区利用率,并从大到小排序

[12:57:35 root@sh-pd-crm-sit-102 ~]# df|tr -s ' '|tail -n +2|cut -d" " -f5,6|sort -nr
15% /boot
4% /run
3% /
1% /data
0% /sys/fs/cgroup
0% /run/user/0
0% /dev/shm
0% /dev
[12:57:42 root@sh-pd-crm-sit-102 ~]# 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值