1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
grep "s|S" /proc/meminfo
grep "^(s|S)" /proc/meminfo
cat /proc/meminfo | grep -i "^s"
cat /proc/meminfo | grep "[1]"
grep -e ^s -e ^S /proc/meminfo
grep -i "^s" /proc/meminfo
grep "[2]" /proc/meminfo
2、显示/etc/passwd文件中不以/bin/bash结尾的行
grep -v "/bin/bash$" /etc/passwd
grep -v ".*<bash>$" /etc/passwd
3、显示用户rpc默认的shell程序
grep "^rpc:" /etc/passwd|cut -d: -f7
getent passwd rpc |cut -d: -f7
grep "^rpc>" /etc/passwd|cut -d: -f7
grep "^rpc" /etc/passwd|cut -d: -f7
grep -w "^rpc" /etc/passwd|cut -d: -f7
grep "^<rpc>" /etc/passwd|grep -o "/[[:alpha:]]+/[[:alpha:]]+$"
4、找出/etc/passwd中的两位或三位数
grep -o "<[0-9]{2,3}>" /etc/passwd
grep -o "[0-9]{2,3}" /etc/passwd
grep -o "<[[:digit:]]{2,3}>" /etc/passwd
grep -w "[0-9]{2,3}" /etc/passwd
5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行
grep "[[:space:]]+[[:space:]]" /etc/grub2.cfg
grep "[3]+" /etc/grub2.cfg |grep "[$]"
grep "[4]+" /etc/grub2.cfg|grep -v "^$"
6、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行
netstat -tan|grep "LISTEN[[:space:]]*$"
7、显示CentOS7上所有系统用户的用户名和UID
cut -d: -f1,3 /etc/passwd|grep "<([1-9]{1}[0-9]{,2})$"
cut -d: -f1,3 /etc/passwd|grep "<([1-9][0-9]?|[1-9][0-9]{2})$"
cut -d: -f1,3 /etc/passwd|grep "<([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$"
8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名同shell名的行
grep "^(.>).<1$" /etc/passwd
grep "^(.)>.<1$" /etc/passwd
grep "^(.):.1$" /etc/passwd
grep "^(.):.<1$" /etc/passwd
9、利用df和grep,取出磁盘各分区利用率,并从大到小排序
df|grep "/dev/sd"|grep -o "[0-9]{,3}%"|grep -o "[0-9]{,3}"|sort -rn
df|grep "/dev/sd"|grep -o "<[0-9]{1,2}>"|sort -nr***不严谨,会有bug--<[0-9]{1,2}>前面的信息可能会匹配到
df|grep "/dev/sd"|grep -o "[0-9]{1,3}%"|grep -o "[0-9]{1,3}"|sort -rn
df|grep "/dev/sd"|tr -s " " "%"|cut -d% -f5|sort -nr
df|grep "/dev/sd"|grep -o "[0-9]{1,2}%"|cut -d% -f1|sort -nr
df|grep /dev/sd|grep -o "[0-9]+%"|grep -o "[0-9]+"|sort -rn
1、显示三个用户root、mage、wang的UID和默认shell
grep -E "^(root|mage|wang)>" /etc/passwd|cut -d: -f1,3,7=grep -E "^(root|mage|wang)" /etc/passwd|cut -d: -f1,3,7
cut -d: -f1,7 /etc/passwd|egrep "(root|mage|wang):"
grep -Ew "^(root|mage|wang)" /etc/passwd|cut -d: -f1,3,7
grep "^<(root|mage|wang)>" /etc/passwd|cut -d: -f1,7
cut -d: -f1,7 /etc/passwd|grep -e "<root>" -e "<mage>" -e "<wang>"
2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
cat /etc/rc.d/init.d/functions |grep -o "^<([[:alpha:]])+>()"
cat /etc/rc.d/init.d/functions |egrep "^<([[:alpha:]]+)>()"
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
basename /etc/rc.d/init.d/functions
echo /etc/rc.d/init.d/functions |egrep -o "[^/]+$"(不严谨,会有bug--/etc/rc.d/init.d/这个就不能匹配)
echo /etc/rc.d/init.d/functions |egrep -o "[^/]+/?$"
echo /etc/rc.d/init.d/functions|egrep -o "/[[:alpha:]]+$"|egrep -o "[[:alpha:]]+"
4、使用egrep取出上面路径的目录名
dirname /etc/rc.d/init.d/functions
echo /etc/rc.d/init.d/functions |egrep -o '^/.*/<'
5、统计last命令中以root登录的每个主机IP地址登录次数
last|egrep "root"|egrep --color=auto -o "([0-9]{1,3}.){3}[0-9]{1,3}"|sort|uniq -c
6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
0-9:echo {0..9}|egrep "[0-9]"
10-99:echo {10..99}|egrep "[1-9]{1}[0-9]{1}"
100-199:echo {100..199}|egrep "1[0-9]{2}"
200-249:echo {200..249}|egrep "2[0-4]{1}[0-9]{1}"
250-255:echo {250..255}|egrep "25[0-5]{1}"
7、显示ifconfig命令结果中所有IPv4地址
基础版:ifconfig ens33|grep -o "([0-9]{1,3}.){3}[0-9]{1,3}"
ifconfig ens33|grep -o "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
ifconfig ens33|grep -Eo "([0-9]{1,3}.){3}[0-9]{1,3}"
ifconfig ens33|egrep -o "([0-9]{1,3}.){3}[0-9]{1,3}"
扩展版:ifconfig|egrep -o "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){2}([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]).([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])>"
全部(包括掩码等):ifconfig|egrep "(([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|2[0-4][0-9]|25[0-5])"
ifconfig|egrep -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])>"
ifconfig|egrep -o "<(([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])>"
8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面
echo welcome to magedu linux|grep -o "."|sort|uniq -c|sort -nr
echo "welcome to magedu linux"|egrep -o "."|sort|uniq -c|sort -r