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

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


学习
学习与分享

道可道,非常道。为学而不得不道。

3.1k 声望
250 粉丝
0 条评论
推荐阅读
SHELL(bash)脚本编程八:技巧
至此,我们介绍了linux系统中常用命令的使用方法,简述了bash程序的使用方法和工作流程。在使用bash编写脚本程序时,熟练掌握这些工具的用法,往往能够达到事半功倍的效果。

vvpale5阅读 5.1k

记一次使用gdb诊断gc问题全过程
上次解决了GC长耗时问题后,系统果然平稳了许多,这是之前的文章《GC耗时高,原因竟是服务流量小?》 然而,过了一段时间,我检查GC日志时,又发现了一个GC问题,如下: 从这个图中可以发现,我们GC有一些尖峰,...

扣钉日记2阅读 1.1k

封面图
使用kubeasz部署高可用kubernetes集群
本实验采用kubeasz作为kubernetes环境部署工具,它是一个基于二进制方式部署和利用ansible-playbook实现自动化来快速部署高可用kubernetes集群的工具,详细介绍请查看kubeasz官方。本实验用到的所有虚拟机默认软...

李朝阳4阅读 728

100 行 shell 写个 Docker
在初接触Docker的时候,我们必须要了解的几个概念就是Cgroup、Namespace、RootFs,如果本身对虚拟化的发展没有深入的了解,那么很难对这几个概念有深入的理解,本文的目的就是通过在操作系统中以交互式的方式去理...

vivo互联网技术2阅读 416

linux中用户登录加载配置文件的过程
shell的类型(站在用户登录登录的角度)登录式shell正常通过某终端登录su - USERNAMEsu -l USERNAME非登录式shellsu USERNAME图形终端下打开命令窗口自动执行的shell脚本用户登录时相关的bash配置文件全局配置文件/...

Dabric阅读 5.3k评论 3

在Linux上查看活跃线程数与连接数
现如今,有两种常见的软件资源几乎成了Java后端程序的标配,即线程池与连接池,但这些池化资源非常的重要,一旦不够用了,就会导致程序阻塞、性能低下,所以有时我们需要看看它们的使用情况,以判断这里是否是瓶颈。

扣钉日记3阅读 990

封面图
9个超有用的 Linux Touch 命令实例讲解
touch 命令用于创建空文件,也用于更改 Linux 系统中现有文件的时间戳。这里更改时间戳意味着更新文件和目录的访问和修改时间。命令语法touch {options} {file}语法选项1) 创建一个空文件touch 命令创建一个空文...

鸠摩智首席音效师1阅读 1.7k

道可道,非常道。为学而不得不道。

3.1k 声望
250 粉丝
宣传栏