资讯专栏INFORMATION COLUMN

linux基础命令介绍五:文本过滤 grep

Jochen / 2332人阅读

摘要:选项将二进制文件当成文本文件处理选项和分别表示排除和包含匹配的文件,表示通配符及用法见基础命令介绍三强大的过滤能力来自于各种选项以及正则表达式的配合,在今后的文章中还有更多的例子。

在linux中经常需要对文本或输出内容进行过滤,最常用的过滤命令是grep

grep [OPTIONS] PATTERN [FILE...]

grep按行检索输入的每一行,如果输入行包含模式PATTERN,则输出这一行。这里的PATTERN是正则表达式(参考前一篇,本文将结合grep一同举例)。

输出文件/etc/passwd中包含root的行:

[root@centos7 temp]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

或者从标准输入获得:

[root@centos7 temp]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

需要注意的地方是:当grep的输入既来自文件也来自标准输入时,grep将忽略标准输入的内容不做处理,除非使用符号-来代表标准输入:

[root@centos7 temp]# cat /etc/passwd | grep root /etc/passwd -
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
(标准输入):root:x:0:0:root:/root:/bin/bash
(标准输入):operator:x:11:0:operator:/root:/sbin/nologin

此时,grep会标明哪些结果来自于文件哪些来自于标准输入。

输出文件/etc/passwd和文件/etc/group中以root开头的行:

[root@centos7 temp]# grep "^root" /etc/passwd /etc/group
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/group:root:x:0:

输出文件/etc/passwd中以/bin/bash结尾的行:

[root@centos7 temp]# grep "/bin/bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
learner:x:1000:1000::/home/learner:/bin/bash

注意以上两个例子中PATTERN被双引号引用起来以防止被shell解析。

输出文件/etc/passwd中不以a-s中任何一个字母开头的行:

[root@centos7 temp]# grep "^[^a-s]" /etc/passwd 
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin

这里需要理解两个^间不同的含义,第一个^表示行首,第二个在[]内部的首个字符^表示取反。

输出文件/etc/passwd中字符0连续出现3次及以上的行(注意转义字符""):

[root@centos7 temp]# grep "0{3,}" /etc/passwd
learner:x:1000:1000::/home/learner:/bin/bash

如输出文件/etc/passwd中以字符rl开头的行:

[root@centos7 temp]# grep "^[rl]" /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
learner:x:1000:1000::/home/learner:/bin/bash

选项-i使grep在匹配模式时忽略大小写:

[root@centos7 temp]# grep -i abcd file 
ABCD
function abcd() {
[root@centos7 temp]#

选项-o表示只输出匹配的字符,而不是整行:

[root@centos7 temp]# grep -oi abcd file 
ABCD
abcd
[root@centos7 temp]#

选项-c统计匹配的行数:

[root@centos7 temp]# grep -oic abcd file 
2
[root@centos7 temp]#

选项-v表示取反匹配,如输出/etc/passwd中不以/sbin/nologin结尾的行:

[root@centos7 temp]# grep -v "/sbin/nologin$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
learner:x:1000:1000::/home/learner:/bin/bash

选项-f FILE表示以文件FILE中的每一行作为模式匹配:

[root@centos7 temp]# cat test
abcd
ABCD
[root@centos7 temp]# grep -f test file 
ABCD
function abcd() {
[root@centos7 temp]# 

选项-x表示整行匹配:

[root@centos7 temp]# grep -xf test file 
ABCD
[root@centos7 temp]#

选项-w表示匹配整个单词:

[root@centos7 temp]# grep here file
here
there
[root@centos7 temp]# grep -w here file
here
[root@centos7 temp]# 

选项-h表示当多个文件时不输出文件名:

[root@centos7 temp]# cat /etc/passwd|grep ^root - /etc/passwd -h
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash

选项-n表示显示行号:

[root@centos7 temp]# grep -n "^[rl]" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
24:learner:x:1000:1000::/home/learner:/bin/bash

选项-A N-B N-C N表示输出匹配行和其"周围行"

-A N 表示输出匹配行和其之后(after)的N行
-B N 表示输出匹配行和其之前(before)的N行
-C N 表示输出匹配行和其之前之后各N行
[root@centos7 temp]# grep -A 2 ^operator /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@centos7 temp]# grep -B2 ^operator /etc/passwd   
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos7 temp]# grep -C1 ^operator /etc/passwd  
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

选项-FPATTERN为它的字面意思匹配(忽略字符的特殊含义),等同于执行命令fgrep

[root@centos7 temp]# grep -F ^root /etc/passwd
[root@centos7 temp]# 

命令无输出

选项-E可以使用扩展的正则表达式,如同执行egrep命令:

[root@centos7 temp]# egrep "^root|^learner" /etc/passwd
root:x:0:0:root:/root:/bin/bash
learner:x:1000:1000::/home/learner:/bin/bash

使用扩展正则表达式意味着不需要转义就能表示字符的特殊含义,包括?,+,{,|,()

选项-P表示使用perl的正则表达式进行匹配
如:

[root@centos7 ~]# echo "helloworld123456"| grep -oP "d+"
123456
[root@centos7 ~]#

perl正则中"d"表示数字,+表示匹配一到多次(同vim)。

选项-a将二进制文件当成文本文件处理:

[root@centos7 ~]# grep -a online /usr/bin/ls
%s online help: <%s>
[root@centos7 ~]#

选项--exclude=GLOB--include=GLOB分别表示排除和包含匹配GLOB的文件,GLOB表示通配符(find及xargs用法见基础命令介绍三):

[root@centos7 temp]# find . -type f | xargs grep --exclude=*.txt --include=test* bash
./test.sh:#!/bin/bash
[root@centos7 temp]#

grep强大的过滤能力来自于各种选项以及正则表达式的配合,在今后的文章中还有更多的例子。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/9617.html

相关文章

  • SegmentFault 技术周刊 Vol.19 - Linux 文本处理三利器

    摘要:所以,本期周刊将介绍命令行世界中最常用的文本处理命令三巨头,让它们带你初步领略命令行的强大。简单的,可以直接理解为是一个数据处理工具,倾向于将一行文本分成数个字段来处理,适合小型数据的处理。 showImg(https://sfault-image.b0.upaiyun.com/226/799/2267992322-586b7c52ed9fc); 之前看过一篇文章《当我们 chmod ...

    hightopo 评论0 收藏0
  • asch相关的linux基础知识分享

    摘要:文件名标识执行某个具有执行权限的文件脚本程序等,是执行时的参数,其它参数只能通过查看脚本内容来查看,估计官方后续会继续优化改进。日志相关在启动后需要查看日志来检查状态,请参考查看文件的末尾几行。 本文针对的人群:会用putty、SecureCRT、xhsell等工具ssh连接到自己的asch服务器上,但不怎么会执行命令的人。高手请绕路~本文主要围绕受托人搭建、维护涉及相关的内容进行Li...

    AbnerMing 评论0 收藏0
  • Java开发人员必须掌握的Linux命令(三)

    摘要:前面写了两篇的命令介绍,开发人员必须掌握的命令一开发人员必须掌握的命令二前段时间看了哈利波特的一些电影,突然想到的命令就像哈利波特电影中的魔法一样,都是有魔力的。 做一个积极的人编码、改bug、提升自己我有一个乐园,面向编程,春暖花开! 学习应该是快乐的,在这个乐园中我努力让自己能用简洁易懂(搞笑有趣)的表达来讲解知识或者技术,让学习之旅充满乐趣,这就是写博文的初心。 前面写了两篇Li...

    dreambei 评论0 收藏0
  • 看完这篇Linux基本的操作就会了

    摘要:前言只有光头才能变强这个学期开了的课程了,授课的老师也是比较负责任的一位。开源,可被定制,开放,多用户的网络操作系统。三常用的命令上面说了一堆的基础概念,这是给我们敲命令之前打了一点基础,在敲命令的同时也会遇到一些比较重要的知识点的。 前言 只有光头才能变强 这个学期开了Linux的课程了,授课的老师也是比较负责任的一位。总的来说也算是比较系统地学习了一下Linux了~~~ 本文章主要...

    AZmake 评论0 收藏0
  • 看完这篇Linux基本的操作就会了

    摘要:前言只有光头才能变强这个学期开了的课程了,授课的老师也是比较负责任的一位。开源,可被定制,开放,多用户的网络操作系统。三常用的命令上面说了一堆的基础概念,这是给我们敲命令之前打了一点基础,在敲命令的同时也会遇到一些比较重要的知识点的。 前言 只有光头才能变强 这个学期开了Linux的课程了,授课的老师也是比较负责任的一位。总的来说也算是比较系统地学习了一下Linux了~~~ 本文章主要...

    Faremax 评论0 收藏0

发表评论

0条评论

Jochen

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<