2017-07-29 14:35:20 @惟愿此心无怨尤
Linux正则表达式总结,后面有一些例题,结合着学习

Regexp(regular expression):由一类特殊字符和文本字符所编写的模式,其中有些字符(元字符)不表示字符字面的意思,而表示控制或者通配的功能。
元字符分类:字符匹配,匹配次数、位置确定,分组。
1.字符匹配
. 匹配任意单个字符
[] 匹配指定范围内的任意单个字符
[^] 匹配指定范围外的任意单个字符
[:alnum:] 或 [0-9a-zA-Z]
[:alpha:] 或 [a-zA-Z]
[:upper:] 或 [A-Z]
[:lower:] 或 [a-z]
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的范围广)
[:cntrl:] 不可打印的控制字符(退格、删除、警铃...)
[:digit:] 十进制数字 或[0-9]
[:xdigit:]十六进制数字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 标点符号

2.匹配次数
设置在字符后面,指定它的匹配次数,规则如下
* 匹配任意次数,包括0次
。* 匹配任意长度的任意字符
\? 匹配其前面的字符0次或1次
\+ 匹配前面的字符至少一次
\{n\} 匹配前面字符n次
\{m,n\} 匹配前面字符至少m次,至多n次
\{m,\} 匹配前面字符至少m次
\{,n\} 匹配前面字符至多n次

3.位置确定
定位出现的位置
^ 行首确定,定位在行的最左侧
$ 行尾确定,定位在行的最右侧
\<、\b 词首确定
\>、\b 词尾确定

4.分组
\(\)将一个或者多个字符捆绑起来,当作一个整体进行处理
接下来以实际例题来对这些正则表达式或者文本处理工具进行解决
下面22题,做完基本对正则能熟练掌握
题目1、找出ifconfig “网卡名” 命令结果中本机的IPv4地址
答案 ifconfig |head -n 2 |tail -n 1|tr -s " " : |cut -d: -f4

题目2、查出分区空间使用率的最大百分比值
答案1 df|tr -s ' ' %|sort -t% -k5 -n|tail -n 1|cut -d% -f5
答案2 df|grep -o "\<[0-9]\{1,3\}\>%"|cut -d% -f1|sort -n |tail -n 1
题目3、查出用户UID最大值的用户名、UID及shell类型
答案 getent passwd |cut -d: -f1,3,7|sort -n -t : -k 2|tail -n 1
题目4、查出/tmp的权限,以数字方式显示
答案1 stat /tmp| head -n 4|tail -n 1|cut -d/ -f1|cut -d'(' -f2
答案2 stat -c %a /tmp/
答案3 stat /tmp|head -n 4|tail -n 1|grep -o "\b[0-7]\{4\}"
题目5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
netstat -tun | grep ESTAB |tr -s " " : |cut -d: -f6 |sort -nr |uniq -c
题目6、显示/proc/meminfo文件中以大小s开头的行
答案1 cat /proc/meminfo|grep "^[Ss]"
答案2 cat /proc/meminfo|grep -i "^s"
答案3 cat /proc/meminfo|grep -e ^s -e ^S
答案4 cat /proc/meminfo|grep "^s\|^S"
答案5 cat /proc/meminfo|grep "^[s\|S]"
题目7、显示/etc/passwd文件中不以/bin/bash结尾的行
答案grep -v "/bin/bash$" /etc/passwd
题目8、显示用户rpc默认的shell程序
答案1 grep "^rpc\>" /etc/passwd | cut -d : -f7
答案2 grep -w "^rpc" /etc/passwd | cut -d : -f7
答案3 getent passwd rpc | cut -d: -f7
题目9、找出/etc/passwd中的两位或三位数
答案 cat /etc/passwd |grep -o "\<[0-9]\{2,3\}\>"
题目10、显示/etc/grub.conf文件中,至少以一个空白
字符开头的且后面存非空白字符的行
答案 cat /etc/grub.conf |grep "^[[:space:]]\{1,\}[^[:space:]]"
题目11、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多
个空白字符结尾的行
答案 netstat -tan|grep "\<LISTEN\>[[:space:]]*$"
题目12、显示所有系统用户的用户名和UID。
以centos6为例子,系统用户为1-499
答案 getent passwd|cut -d: -f1,3|grep "\<[1-9][0-9]\>\|\<[1-4][0-9]\{1,2\}\>\|\<[1-9]\>"
以centos7为例子,系统用户为1-999
答案 getent passwd |cut -d: -f1,3 |grep "\<[[:digit:]]\{1,3\}\>"$
题目13、添加用户bash、testbash、basher、sh、nologin(其shell
为/sbin/nologin),找出/etc/passwd用户名同shell名的行
答案 cat /etc/passwd | grep "\(^.*\)\>.*\/\1$"
题目14、仅利用df和grep和sort,取出磁盘各分区利用率,并从大到小排序
答案1 df |grep ^/dev/sd |grep -o "\b[[:digit:]]\{1,3\}\b%"|sort -rn
答案2 df|grep ^/dev/sd|grep -o "\<[0-9]\{1,3\}%"|grep -o "[0-9]\{1,3\}"|sort rnr
题目15、显示两个个用户root、user的UID和默认shell
答案1 cat /etc/passwd|grep -E "^(root|user)\>"|cut -d : -f3,7
答案2 cat /etc/passwd|grep -E -w "^(root|user)"|cut -d : -f3,7
答案3 getent passwd root user|cut -d: -f3,7

题目16、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
答案1 cat /etc/rc.d/init.d/functions |grep "^\(_\|[[:alpha:]]\)\+("
答案2 cat /app/functions |grep -o "^.*[[:graph:]]()"
答案3 cat /etc/rc.d/init.d/functions |egrep "^.*[^[:space:]]\(\)"
答案4 cat /etc/rc.d/init.d/functions |egrep -o "^.*\>\(\)"
题目17、取出/etc/rc.d/init.d/functions中其基名
答案1 basename /etc/rc.d/init.d/functions
答案2 echo "/etc/rc.d/init.d/functions"|cut -d/ -f5
答案3 echo "/etc/rc.d/init.d/functions" |grep -o "[^/]\+/\?$"
答案4 echo /etc/rc.d/init.d/functions |egrep "[^/]+/?$" -o
题目18、取出/etc/rc.d/init.d/functions路径的目录名
答案1 dirname /etc/rc.d/init.d/functions
答案2 echo "/etc/rc.d/init.d/functions"|grep -o "/.*/"
答案 3echo /etc/rc.d/init.d/functions/ | egrep -o ".*/\<"
答案4 echo /etc/rc.d/init.d/functions |egrep -o ".*/." |egrep -o ".*/" /etc/rc.d/init.d/

题目19、统计last命令中以root登录的每个主机IP地址登录次数
答案 last root|tr -s ' ' ':'|cut -d: -f3|sort -n|uniq -c
答案 last |grep "^root\>"|egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" |sort|uniq -c

题目20、利用正则表达式分别表示0-9、10-99、100-199、200-249、250-255
0-9:echo {0..500}|grep -o "\<[0-9]\>"|tr '\n' ' ';echo
10-99:echo {0..500}|grep -o "\<[1-9]\{2\}\>\|\<10\>"|tr '\n' ' ';echo
100-199:echo {0..500}|grep -o "\<1[0-9]\{2\}"|tr '\n' ' ';echo
200-249:echo {0..500}|grep -o "\b2[0-4][0-9]"|tr '\n' ' ';echo
250-255:echo {0..500}|grep -o "\b25[0-5]"|tr '\n' ' ';echo





题目21、显示ifconfig命令结果中所有IPv4地址
答案 ifconfig |grep "inet addr"|tr -s ' ' ':'|cut -d: -f4
答案 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])\>"

题目22、将此字符串:welcome to my home 中的每个字符去重并排序,重复次数多的排到前面
答案 echo "welcome to my home"|grep -o "[[:graph:]]"|sort -n|uniq -c|sort -nr
答案 echo "welcome to magedu linux"|grep -o . |sort|uniq -c |sort -nr

浙公网安备 33010602011771号