标签:egrep
我们先来看下egrep的相关参数:
grep
作用:通过一个指定的模式匹配指定的行
常用选项:
    -E 扩展正则表达式匹配 相当于egrep命令
    -o 只获取匹配到的内容
-v 匹配正则表达式相反的内容
    --color=auto 将搜索到的内容高亮
    -A num 匹配到搜索到的行以及该行下面的num行
    -B num 匹配到搜索到的行以及该行上面的num行 
    -C num 匹配到搜索到的行以及上下各num行
    -n 显示文件的行数
    -r 递归目录搜索指定内容的文件
    -i 正则表达式内容不区分大小写
基本正则表达式说明:
字符匹配:
.: 匹配任意单个字符;
[]:匹配指定范围内的任意单个字符;
[^]:匹配指定范围内的任意单个字符;
     [:lower:] 匹配小写字母
    [:upper:] 匹配大写字母
    [:alpha] 匹配所有字母
    [:digit:] 匹配所有数字
    [:alnum:] 匹配所有数字字母 [:alpha:]和[:alpha:]合二为一
    [:space] 代表空格字符 例如:tab,换行,空格之类的
[:punct:] 代表标点符号 例如:‘! " # $ % & ‘ ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ‘ { | }
次数匹配:用于要指定其次数的字符的后面;
*: 任意次;
\?:0或1次;
\+:1或多次;
\{m\}:精确限制为m次;
\{m,n\}: 至少m次,至多n次,[m,n]
\{0,n\}:至多n次;
\{m,\}:至少m次;
.*: 匹配任意长度的任意字符;
位置锚定:
^: 行首锚定;用于模式的最左侧;
$: 行尾锚定;用于模式的最右侧;
\<, \b: 词首锚定;用于表示单词的模式的左侧;
\>, \b:词尾锚定;用于表示单词的模式的右侧;
^$: 空白行;
分组:\(\)
分组的小括号中的模式匹配到的内容,会在执行过程中被正则表达式引擎记录下来,并保存内置的变量中;这些变量分别是\1, \2, ...
    \1: 从左侧起,第一个左括号,以及与之配对的右括号中间的模式所匹配到的内容; \2以此类推。
扩展正则表达式的元字符:
字符匹配:
    同基本正则表达式
次数匹配
*: 0次或多次
?: 0次或1次;
+: 1次以上;
{m}: 精确匹配m次;
{m,n}: 至少m次,至多n次;
锚定:
^: 锚定行首
$: 锚定行尾
\<, \b: 词首锚定;用于表示单词的模式的左侧;
\>, \b:词尾锚定;用于表示单词的模式的右侧;
分组:
同基本正则表达式
支持或的概念
    a|b:匹配a或者b
1、显示/etc/passwd文件中以bash结尾的行;
[oracle@HZ-CDN-1 ~]$ cat /etc/passwd | grep "bash$" root:x:0:0:root:/root:/bin/bash robin:x:601:601::/home/robin:/bin/bash oracle:x:602:602::/home/oracle:/bin/bash
2、显示/etc/passwd文件中的两位数或三位数;
[oracle@HZ-CDN-1 ~]$ cat /etc/passwd | egrep -o "\<([1-9][0-9]|[0-9][0-9][0-9])\>" 12 10 14 11 12 100 13 30 14 50 99 99 81 81 69 69 …… 502 502 600 600 601 601 602 602
3、显示‘netstat -tan‘命令结果中以‘LISTEN’后跟0个、1个或多个空白字符结尾的行;
[oracle@HZ-CDN-1 ~]$ netstat -tan | grep --color "LISTEN[[:space:]]*$" tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:65422 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
4、添加用户bash,testbash, basher以及nologin用户(nologin用户的shell为/sbin/nologin);而后找出/etc/passwd文件中用户名同shell名的行;
[root@HZ-CDN-1 oracle]# useradd bash [root@HZ-CDN-1 oracle]# useradd testbash [root@HZ-CDN-1 oracle]# useradd basher [root@HZ-CDN-1 oracle]# useradd nologin -s /sbin/nologin [root@HZ-CDN-1 ~]# grep --color -E ‘^([[:alnum:]]+):.+\1$‘ /etc/passwd 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:603:603::/home/bash:/bin/bash nologin:x:606:606::/home/nologin:/sbin/nologin
[root@HZ-CDN-1 ~]# egrep ‘^([[:alnum:]]+):.*\1$‘ /etc/passwd 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:603:603::/home/bash:/bin/bash nologin:x:606:606::/home/nologin:/sbin/nologin
[root@HZ-CDN-1 ~]# grep "^\(\<[[:alnum:]]\+\>\).\+\1$" /etc/passwd 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:603:603::/home/bash:/bin/bash nologin:x:606:606::/home/nologin:/sbin/nologin
5、显示当前系统上root、centos或user1用户的默认的shell和UID;
[root@HZ-CDN-1 oracle]# cat /etc/passwd | egrep "^(\<user1|root|centos\>)" | awk -F‘:‘ ‘{print $1,$3,$7}‘
root 0 /bin/bash
centos 607 /bin/bash
user1 608 /bin/bash6、找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的
行;
[root@HZ-CDN-1 ~]# egrep --color ‘\<[[:alnum:]_]+\>\(\)‘ /etc/rc.d/init.d/functions    
fstab_decode_str() {
checkpid() {
__readlink() {
__fgrep() {
__umount_loop() {
__umount_loopback_loop() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
action_silent() {
strstr() {
confirm() {
get_numeric_dev() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {
key_is_random() {
find_crypto_mount_point() {
init_crypto() {7、使用echo输出一个路径,而后egrep找出其路径基名;进一步地:使用egrep取出其目录名;
[oracle@HZ-CDN-1 ~]$ echo "/etc/rc.d/init.d/functions" | egrep -o ‘([^/]*)$‘ functions [root@HZ-CDN-1 ~]# echo "/etc/rc.d/init.d/functions" | egrep -o ‘^(.*)/‘ /etc/rc.d/init.d/
8、找出ifconfig命令执行结果中1-255之间的数字;
[oracle@HZ-CDN-1 ~]$ ifconfig | egrep -o "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" | sort 1 1 1 1 10 10 100 103 121 121 248 ……省略
本文出自 “方文俊的IT技术博客” 博客,请务必保留此出处http://fangwenjun.blog.51cto.com/7806977/1689508
标签:egrep
原文地址:http://fangwenjun.blog.51cto.com/7806977/1689508