grep 判断不是正则的_Grep & 正则表达式

Grep 介绍

Grep 命令主要用于过滤文本,grep 家族如下:

grep: 在文件中全局查找指定的正则表达式,并打印所有包含该表达式的行

egrep:扩展的 egrep,支持更多的正则表达式元字符

fgrep:固定 grep(fixed grep),有时也被称作快速(fast grep),它按字面解释所有的字符

Grep 命令格式如下:

grep [选项] PATTERN 文件1 文件2 ...

[root@wqh ~]# grep 'root' /etc/passwd

[root@wqh ~]# fgrep 'bash' /etc/passwd

找到:: grep返回的退出状态为 0

没找到: grep返回的退出状态为 1

找不到指定文件: grep返回的退出状态为 2

Grep 命令的输入可以来自标准输入或管道,而不仅仅是文件,例如:

ps aux |grep 'nginx'

Grep 选项

-n, --line-number在过滤出的每一行前面加上它在文件中的相对行号

-o, --only-matching只显示匹配的内容

-q, --quiet, --silent 静默模式,没有任何输出,得用$?来判断执行成功没有,即有没有过滤到想要的内容

--color颜色

-i, --ignore-case忽略大小写

-A, --after-context=NUM 如果匹配成功,则将匹配行及其后n行一起打印出来

-B, --before-context=NUM 如果匹配成功,则将匹配行及其前n行一起打印出来

-C, --context=NUM如果匹配成功,则将匹配行及其前后n行一起打印出来

-c, --count如果匹配成功,则将匹配到的行数打印出来

-v, --invert-match反向查找,只显示不匹配的行

-w匹配单词

-E等于egrep,扩展

-l, --files-with-matches 如果匹配成功,则只将文件名打印出来,失败则不打印

通常 -rl 一起用,获取匹配到的文件列表,grep -rl 'root' /etc

-R, -r, --recursive递归

Grep 命令选项示例:

# 1、-n 输出行号

[root@wqh ~]# grep -n 'root' /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

10:operator:x:11:0:operator:/root:/sbin/nologin

[root@wqh ~]#

# 2、-o 只输出匹配的内容

[root@wqh ~]# grep -o 'root' /etc/passwd

root

root

root

root

[root@wqh ~]#

# 3、-q 静默输出

[root@wqh ~]# grep -q 'root' /etc/passwd

[root@wqh ~]# echo $?

0

# 4、--color 匹配到的字符串会加颜色

[root@wqh ~]# alias grep

alias grep='grep --color=auto'

[root@wqh ~]#

# 5、-i 忽略大小写

[root@wqh ~]# echo "WQH" |grep -i wqh

WQH

[root@wqh ~]#

# 6、-A\-B\-C 输出 之后/之前/上下文 的 N 行

[root@wqh ~]# grep -A 2 'root' /etc/passwd

[root@wqh ~]# grep -B 2 'root' /etc/passwd

[root@wqh ~]# grep -C 2 'root' /etc/passwd

# 7、-c 统计行数

[root@wqh ~]# grep -c 'root' /etc/passwd

2

[root@wqh ~]#

# 8、-v 反向输出

[root@wqh ~]# ps aux | grep nginx |grep -v grep

[root@wqh ~]#

[root@wqh ~]# ps aux | grep [n]ginx

[root@wqh ~]#

# 9、-w 界定单词边界,以空格、冒号、分号等字符为分隔符

[root@wqh ~]# netstat -an |grep -w 80

tcp6 0 0 :::80 :::* LISTEN

[root@wqh ~]# netstat -an |grep '\<80\>'

tcp6 0 0 :::80 :::* LISTEN

[root@wqh ~]# netstat -an |grep '\b80\b'

tcp6 0 0 :::80 :::* LISTEN

# 10、-rl

[root@wqh ~]# grep -rl 'root' /etc # 将/etc目录下所有包含'root'内容的文件都列出来

正则表达式

正则表达式介绍

正则表达式(Regular Expression,在代码中常简写为 regex、regexp 或 RE),是计算机科学的一个概念,正则表达式由元字符组成,通常被用来检索、替换那些符合某个模式(规则)的文本(许多程序设计语言都支持利用正则表达式进行字符串操作)。

正则表达式元字符

元字符,是一类可以表达出超越其字面本身含义的特殊字符 :

# Shell 元字符(也称为通配符):

由 shell 解释器来解析,如 rm -rf *.pdf,元字符 * Shell 将其解析为任意多个字符

# 正则表达式元字符:

由各种执行模式匹配操作的程序来解析,比如 vi、grep、sed、awk

# 例如:vim 示例:

:1,$ s/tom/wqh/g

# 如 anatomy 、tomatoes 及 tomorrow 中的 "tom" 被替换了,而 "Tom" 确没被替换

# 用以下方法可以替换大小写字母

:1,$ s/\/wqh/g

# ====== 常用命令,$ 符号可以让 \t 生效 ====== #

egrep -v $'^#|^[ \t]*$' passwd

基本正则元字符集

元字符 功能 示例

^ 行首^love

$ 行尾 love$

. 除了换行符以外的任意单个字符 l..e

* 前导字符的零个或多个 ab*love

.* 所有字符a.*love

[] 字符组内的任一字符[lL]ove

[^] 对字符组内的每个字符取反(不匹配字符组内的每个字符) [^a-z0-9]ove

^[^] 非字符组内的字符开头的行

[a-z] 小写字母

[A-Z] 大写字母

[a-Z] 小写和大写字母

[0-9] 数字

\ 用来转义元字符 love\.

\< 词首定位符 单词一般以空格或特殊字符做分隔、连续的字符组成 \

\> 词尾定位符 love\>

\(..\) 匹配稍后将要使用的字符的标签 \(love\)able\1er

s/(.*)/\1/g 后项引用 :1,$ s/\(192.168.11\).66/\1.50/g

x\{m\} 字符 x 重复出现 m 次 e\{3\}

x\{m,\} 字符 x 重复出现 m 次以上 e\{3,\}

x\{m,n\} 字符 x 重复出现 m 到 n 次 e\{3,6\}

# ====指定匹配前一个字符的精确次数或者范围====== #

* 左侧那个字符出现0或无穷次,{0,}

+ 左侧那个字符出现1或无穷次,{1,}

? 左侧那个字符出现0到1次,{0,1}

基本正则表达式食用方法:

# 1、^ 行首

[root@wqh ~]# grep '^root' /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@wqh ~]#

# 2、$ 行尾

[root@wqh ~]# grep 'bash$' /etc/passwd

root:x:0:0:root:/root:/bin/bash

user1:x:1002:1003::/home/user1:/bin/bash

wqh1:x:198:1005::/home/wqh1:/bin/bash

gg:x:1004:1006::/home/gg:/bin/bash

wqh:x:1005:1007::/home/wqh:/bin/bash

tom:x:1006:1008::/home/tom:/bin/bash

[root@wqh ~]#

# 3、. 除了换行符以外的任意单个字符

[root@wqh ~]# grep 'r..t' /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@wqh ~]#

# 4、* 前导字符的零个或多个

[root@wqh ~]# cat a.txt

a

ab

abb

abbb

bbbbb

[root@wqh ~]# grep 'ab*' a.txt

a

ab

abb

abbb

[root@wqh ~]#

# 5、.* 所有字符 = 贪婪

[root@wqh ~]# cat a.txt

a123+-*/c11113333c

a1c

a77Ac

a23333c

ac

111

222

333

[root@wqh ~]# grep 'a.*c' a.txt

a123+-*/c11113333c

a1c

a77Ac

a23333c

ac

[root@wqh ~]#

# 5.5 .*? => 非贪婪,默认情况下,grep不支持非贪婪修饰符,但您可以使用 grep -P 来使用 Perl 语法来支持 .*?

[root@wqh ~]# cat a.txt

"我他妈的是百度"

"我特么的是新浪"

[root@wqh ~]#

[root@wqh ~]# grep -o 'href=".*"' a.txt # 贪婪

href="http://www.baidu.com">"我他妈的是百度"

href="http://www.sina.com.cn">"我特么的是新浪"

[root@wqh ~]#

[root@wqh ~]# grep -oP 'href=".*?"' a.txt # 非贪婪

href="http://www.baidu.com"

href="http://www.sina.com.cn"

[root@wqh ~]#

# 6、[] 字符组内的任一字符

# 7、[^] 对字符组内的每个字符取反(不匹配字符组内的每个字符),放在外面是以某字符开头

[root@wqh ~]# cat a.txt

a1c

a2c

a33c

aAc

aZc

[root@wqh ~]# grep 'a[0-9]c' a.txt

a1c

a2c

[root@wqh ~]# grep 'a[^0-9]c' a.txt

aAc

aZc

[root@wqh ~]#

[root@wqh ~]# grep 'a[0-9][0-9]c' a.txt

a33c

[root@wqh ~]#

# 8、^[^] 非字符组内的字符开头的行

[root@wqh ~]# cat a.txt

a1c

a2c

a33c

aAc

aZc

[root@wqh ~]# grep '^[^0-9]..$' a.txt

a1c

a2c

aAc

aZc

[root@wqh ~]#

# 9、[a-z] 小写字母

# 10、[A-Z] 大写字母

# 11、[a-Z] 小写和大写字母

# 12、[0-9] 数字

# 13、\< 单词头 单词一般以空格或特殊字符做分隔,连续的字符串被当做单词

# 14、\> 单词尾

[root@wqh ~]# netstat -an |grep -w 80

tcp6 0 0 :::80 :::* LISTEN

[root@wqh ~]# netstat -an |grep '\<80\>'

tcp6 0 0 :::80 :::* LISTEN

# 15、\< \> 都可以可以用 \b 代替

[root@wqh ~]# netstat -an |grep '\b80\b'

tcp6 0 0 :::80 :::* LISTEN

Grep 匹配换行符和制表符时,需要 $ 符号,否则不识别:

[root@wqh ~]# echo -e "a\nb" |grep $'a\nb'

a

b

[root@wqh ~]#

[root@wqh ~]# echo -e "a\tb" |grep $'a\tb'

ab

# ====== 常用命令,$ 符号可以让 \t 生效 ====== #

egrep -v $'^#|^[ \t]*$' xxxx.conf

扩展正则元字符集

# 扩展正则元字符

+匹配一个或多个前导字符 [a-z]+ove

?匹配零个或一个前导字符 lo?ve

a|b匹配a或blove|hate

()组字符love(able|rs) (wqh)+

(..)(..)\1\2 标签匹配字符 (love)able\1er

x{n} x出现n次 e{3}

x{n,} x出现n次至无穷次 e{3,}

x{n,m} x出现n次至m次 e{3,6}

# 若想使用扩展正则

grep -E 或 egrep 或 转义 \

sed 加 -r 参数 或转义 \

AWK 直接支持大多数扩展正则,更多支持需要加选项 --posix 选项

扩展正则元字符食用方法:

# ====================== grep 扩展正则示例 ====================== #

[root@wqh ~]# cat a.txt

a

ab

abb

abbb

abbbb

abbbbb

bbbbbbb

[root@wqh ~]# grep 'ab{2,4}' a.txt # 默认不支持扩展正则,所以没效果

[root@wqh ~]# egrep 'ab{2,4}' a.txt

abb

abbb

abbbb

abbbbb

[root@wqh ~]#

# ===================== sed 扩展正则示例 ====================== #

[root@wqh ~]# sed -n '/roo?/p' /etc/passwd # 默认不支持扩展正则?

[root@wqh ~]# sed -n '/roo\?/p' /etc/passwd # 可以用\转义扩展正则符号?

有结果,结果略...

[root@wqh ~]# sed -rn '/roo?/p' /etc/passwd # 也可以加 -r 选项

有结果,结果略...

[root@wqh ~]#

# ====================== awk 扩展正则示例 ====================== #

[root@wqh ~]# cat a.txt

a

ab

abb

abbb

abbbb

abbbbb

bbbbbbb

[root@wqh ~]# awk '/ab{1,3}/{print}' a.txt

ab

abb

abbb

abbbb

abbbbb

[root@wqh ~]# awk --posix '/ab{1,3}/{print}' a.txt

ab

abb

abbb

abbbb

abbbbb

[root@wqh ~]#

fa40e85372fe2ccec7b6a36bb5182e02.png

Grep & Egrep 总结

grep 使用基本元字符集^, $, ., *, [], [^], \< \>,\(\),\{\}

egrep(grep -E) 使用扩展元字符集 ?, +, { }, |, ( )

# 注:grep也可以使用扩展集中的元字符,仅需要对这些元字符前置一个反斜线

\w所有字母与数字,称为字符[a-zA-Z0-9] 'l[a-zA-Z0-9]*ve' 'l\w*ve'

\W所有字母与数字之外的字符,称为非字符 'love[^a-zA-Z0-9]+' 'love\W+'

\b词边界'\blove\b' '\'

Posix 定义的字符分类

# 表达式 功能 示例

[:alnum:] 字母与数字字符 [[:alnum:]]+

[:alpha:] 字母字符(包括大小写字母) [[:alpha:]]{4}

[:blank:] 空格与制表符 [[:blank:]]*

[:digit:] 数字字母 [[:digit:]]?

[:lower:] 小写字母 [[:lower:]]{5,}

[:upper:] 大写字母 [[:upper:]]+

[:punct:] 标点符号 [[:punct:]]

[:space:] 包括换行符,回车等在内的所有空白[[:space:]]+

# 详解

[:alnum:] Alphanumeric characters.

匹配范围为 [a-zA-Z0-9]

[:alpha:] Alphabetic characters.

匹配范围为 [a-zA-Z]

[:blank:] Space or tab characters.

匹配范围为 空格和TAB键

[:cntrl:] Control characters.

匹配控制键 例如 ^M 要按 ctrl+v 再按回车 才能输出

[:digit:] Numeric characters.

匹配所有数字 [0-9]

[:graph:] Characters that are both printable and visible. (A space is print-

able, but not visible, while an a is both.)

匹配所有可见字符 但不包含空格和TAB 就是你在文本文档中按键盘上能用眼睛观察到的所有符号

[:lower:] Lower-case alphabetic characters.

小写 [a-z]

[:print:] Printable characters (characters that are not control characters.)

匹配所有可见字符 包括空格和TAB

能打印到纸上的所有符号

[:punct:] Punctuation characters (characters that are not letter, digits, con-

trol characters, or space characters).

特殊输入符号 +-=)(*&^%$#@!~`|\"'{}[]:;?/>.

注意它不包含空格和TAB

这个集合不等于^[a-zA-Z0-9]

[:space:] Space characters (such as space, tab, and formfeed, to name a few).

[:upper:] Upper-case alphabetic characters.

大写 [A-Z]

[:xdigit:] Characters that are hexadecimal digits.

16进制数 [0-f]

# 使用方法:

[root@wqh ~]# grep --color '[[:alnum:]]' /etc/passwd

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值