grep,egrep 及正则表达式(2)

grep,egrep 及正则表达式

# grep “bash$”/etc/passwd

# grep –E “bash$”/etc/passwd

# egrep “bash$” /etc/passwd

2、显示/etc/passwd文件中的两位数或三位数

grep,egrep 及正则表达式

# grep “\<[[:digit:]]\{2,3\}\>”/etc/passwd

# egrep “\<[0-9]{2,3}\>”/etc/passwd

3、显示`netstat -tan`命令结果中以‘LISTEN’后跟0个、1个或者多个空白字符结尾的行

grep,egrep 及正则表达式

# netstat –tan | grep “LISTEN[[:space:]]*$”

# netstat –tan | egrep “LISTEN[[:space:]]*$”

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

grep,egrep 及正则表达式

# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd

# egrep "^([[:alnum:]]+\>).*\1$" /etc/passwd

5、显示当前系统上root、CentOS或者user1用户的默认shell和UID (请事先创建这些用户,若不存在)

grep,egrep 及正则表达式

# grep -E "^(root|centos|user1)\>" /etc/passwd |cut -d:-f3,7

# egrep  "^(root|centos|user1)\>"/etc/passwd |cut -d: -f3,7

6、找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行

grep,egrep 及正则表达式

# egrep -o "\<[[[:alnum:]_]+\>\(\)"/etc/rc.d/init.d/functions

7、使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名

grep,egrep 及正则表达式

# echo/etc/sysconfig/network-scripts/ | egrep -o "[[:alnum:]-]+/?$"

# echo /etc/sysconfig/network-scripts| egrep -o "(/.*/)"

8、找出ifconfig命令执行结果中1-255之间的数字

grep,egrep 及正则表达式

# ifconfig | egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"

Linux 基础入门教程----正则表达式基础   

Linux正则表达式sed 详述 

Linux正则表达式特性及BRE与ERE的区别

grep使用简明及正则表达式

正则表达式的用法

正则表达式之零宽断言

Linux中正则表达式与文件格式化处理命令(awk/grep/sed)

基础正则表达式

常用正则表达式整理

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/15897.html