微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

grep及正则表达式系列---01

grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包括grep、egrepfgrep

grep:根据模式搜索文本,并将符合模式的文本行显示出来。

Pattern:由文本字符和正则表达式的元字符组合而成的匹配条件。

grep [OPTIONS] PATTERN [FILE...]

例子:# grep 'root' /etc/passwd

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

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

grep常用相关选项:

-i:忽略大小写

例子:# grep -i 'root' /etc/passwd

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

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


--color:带颜色显示


例子:# grep --color 'root' /etc/passwd

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

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


注意:也可以给grep取别名带颜色显示

例子:# aliasgrep='grep --color '

# grep 'root' /etc/passwd

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

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


-v:反向查找,显示没有被模式匹配到的行。

例子:# grep -v 'root' /etc/passwd

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

......

-o:只显示被模式匹配到的字符串。

例子:# grep -o 'root' /etc/passwd

root

root

root

root

原文地址:https://www.jb51.cc/regex/361903.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐