您的位置:首页 > 运维架构 > Linux

linux 常用命令 - grep

2017-09-08 09:50 162 查看

匹配多个关键词

用法:grep -E ‘your regular expression’ file

举例:我们拿冰与火之歌第七季第七集的部分字幕举个例子。

GameOfThrones.txt

30
00:04:14,370 --> 00:04:19,370
{\an8}{\pos(94.16,212.825)}app.YYeTs.com

37
00:00:08,070 --> 00:00:11,100
Little Theon! Come and get her.

38
00:00:19,300 --> 00:00:22,650
It's an invitation... to King's Landing.

39
00:00:22,650 --> 00:00:25,820
You will represent my interests at this gathering as you see them.

40
00:00:25,820 --> 00:00:28,540
It's not safe leaving you with Littlefinger.

41
00:00:28,540 --> 00:00:29,870
She's your sister.


匹配GameOfThrones.txt中含有’sister’或’King’的行

[~/shell]$ grep -E 'sister|King' GameOfThrones.txt
It's an invitation... to King's Landing.
She's your sister.


匹配GameOfThrones.txt中含有’sister’或’King’或’Little Theon’的行

注意’Little Theon’中间有个空格,这个时候就要用到正则表达式了Little.Theon,正则表达式中.代表任意字符。

[~/shell]$ grep -E 'sister|King|Little.Theon' grep2.txt
Little Theon! Come and get her.
It's an invitation... to King's Landing.
She's your sister.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息