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

简介:

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


  
  
  1. grep [OPTIONS] PATTERN [FILE...] 

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

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


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

或者从标准输入获得:


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

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


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

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

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


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

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


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

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

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


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

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

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


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

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


  
  
  1. [root@centos7 temp]# grep "^[r,l]" /etc/passwd 
  2. root:x:0:0:root:/root:/bin/bash 
  3. lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
  4. learner:x:1000:1000::/home/learner:/bin/bash  

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


  
  
  1. [root@centos7 temp]# grep -i abcd file  
  2. ABCD 
  3. function abcd() { 
  4. [root@centos7 temp]#  

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


  
  
  1. [root@centos7 temp]# grep -oi abcd file  
  2. ABCD 
  3. abcd 
  4. [root@centos7 temp]#  

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


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

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


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

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


  
  
  1. [root@centos7 temp]# cat test 
  2. abcd 
  3. ABCD 
  4. [root@centos7 temp]# grep -f test file  
  5. ABCD 
  6. function abcd() { 
  7. [root@centos7 temp]#   

选项-x表示整行匹配:


  
  
  1. [root@centos7 temp]# grep -xf test file  
  2. ABCD 
  3. [root@centos7 temp]#  

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


  
  
  1. [root@centos7 temp]# grep here file 
  2. here 
  3. there 
  4. [root@centos7 temp]# grep -w here file 
  5. here 
  6. [root@centos7 temp]#   

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


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

选项-n表示显示行号:


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

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


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

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


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

命令无输出

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


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

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

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

如:


  
  
  1. [root@centos7 ~]# echo "helloworld123456"| grep -oP "\d+" 
  2. 123456 
  3. [root@centos7 ~]#  

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

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


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

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


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

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




作者:vvpale
来源:51CTO

目录
相关文章
|
7天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
50 23
Linux系统之whereis命令的基本使用
|
21天前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
34 11
|
2月前
|
Linux Shell
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
100 14
Linux 10 个“who”命令示例
|
2月前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
203 20
|
2月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
143 8
|
2月前
|
监控 Linux 数据处理
Linux grep技巧 结合awk查询
结合 `grep` 和 `awk`,可以实现灵活、高效的文本处理和数据分析。`grep` 用于快速过滤符合条件的行,`awk` 用于进一步处理和提取数据。这种组合使用在日志分析、数据处理和系统监控等场景中尤为常见。掌握这两者的基本用法和组合技巧,可以大大提升在 Linux 环境下的工作效率。
61 7
|
2月前
|
网络协议 Linux 应用服务中间件
kali的常用命令汇总Linux
kali的常用命令汇总linux
128 7
|
3月前
|
Linux 数据库
Linux中第一次使用locate命令报错?????
在Linux CentOS7系统中,使用`locate`命令时出现“command not found”错误,原因是缺少`mlocate`包。解决方法是通过`yum install mlocate -y`或`apt-get install mlocate`安装该包,并执行`updatedb`更新数据库以解决后续的“can not stat”错误。
61 9
|
3月前
|
监控 网络协议 Linux
Linux netstat 命令详解
Linux netstat 命令详解
|
3月前
|
运维 监控 网络协议
运维工程师日常工作中最常用的20个Linux命令,涵盖文件操作、目录管理、权限设置、系统监控等方面
本文介绍了运维工程师日常工作中最常用的20个Linux命令,涵盖文件操作、目录管理、权限设置、系统监控等方面,旨在帮助读者提高工作效率。从基本的文件查看与编辑,到高级的网络配置与安全管理,这些命令是运维工作中的必备工具。
290 3