linux技术专栏

linux命令grep

2019-06-28  本文已影响0人  huxt

1.grep的规则表达式(正则一定要转义)

grep搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。

2.grep常见命令参数

-n 打印行号

    grep -n ".*" h.txt        所有打印行号

    grep -n "root" h.txt    匹配的内容显示行号

-v 不包括

-E  表示过滤 多个参数

    grep -Ev "sshd|network|crond|sysstat|" 

-o:仅打印你需要的东西,默认打印正行

    grep -o "hello" h.txt

-i:忽略大小写

    grep -i "hello" h.txt

-c: 用于统计文中出现的次数

--color=auto 过滤字段添加颜色

\b:作为边界符,边界只包含特定字符的行

grep "\boldboy\b" /etc/passwd -->只过滤包含oldboy的行

从多个文件中查找关键词

grep "omc" /etc/passwd /etc/shadow

数据去重:cat log | sort | uniq

上一篇 下一篇

猜你喜欢

热点阅读