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

种方法)

  1. grep -i "^s" /proc/meminfo
  1. grep "^[sS]" /proc/meminfo

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

  1. grep -v "/bin/bash$" /etc/passwd

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

  1. grep "^rpc\>" /etc/passwd | cut -d":" -f7

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

  1. grep -o "\<[0-9]\{2,3\}\>" /etc/passwd

5 、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白

字符开头的且后面存非空白字符的行

  1. grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg

6 、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多

个空白字符结尾的行

  1. netstat -tan | grep "LISTEN[[:space:]]*$"

7 、显示CentOS7上所有系统用户的用户名和UID

  1. cut -d: -f1,3 /etc/passwd | grep "\<[0-9]\{1,3\}$"
  2. 结尾不用\>是因为用户名如果是123的话也包括,用$锚定词尾
  3. \<可以用\b替换

8 、添加用户bash 、testbash 、basher 、sh 、nologin( 其shell

为/sbin/nologin), 找出/etc/passwd用户名同shell名的行

  1. useradd bash
  2. useradd testbash
  3. useradd basher
  4. useradd sh
  5. useradd nologin -s /sbin/nologin
  6. grep "^\(.*\):.*\<\1$" /etc/passwd
  7. grep "^\(.*\)\>.*\<\1$" /etc/passwd

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

  1. df | grep "/dev/sd" | grep -o "\<[[:digit:]]\{1,3\}%" | grep -o "[[:digit:]]\{1,3\}" | sort -nr

10、取出ip地址

  1. ifconfig ens37 | grep -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"
  2. ifconfig ens37 | grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}"