iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >linux怎么对文本或输出内容进行过滤
  • 115
分享到

linux怎么对文本或输出内容进行过滤

2023-06-09 17:06:25 115人浏览 薄情痞子
摘要

这篇文章主要讲解了“linux怎么对文本或输出内容进行过滤”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“linux怎么对文本或输出内容进行过滤”吧!在linux中经常需要对文本或输出内容进行

这篇文章主要讲解了“linux怎么对文本或输出内容进行过滤”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“linux怎么对文本或输出内容进行过滤”吧!

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

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

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

[root@Centos7 temp]# grep root /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin

或者从标准输入获得:

[root@centos7 temp]# cat /etc/passwd | grep rootroot:x:0:0:root:/root:/bin/bashoperator: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/passwdroot:x:0:0:root:/root:/bin/bashlearner: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/nologintcpdump:x:72:72::/:/sbin/nologin

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

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

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

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

[root@centos7 temp]# grep "^[r,l]" /etc/passwdroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinlearner:x:1000:1000::/home/learner:/bin/bash

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

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

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

[root@centos7 temp]# grep -oi abcd file ABCDabcd[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/passwdroot:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltlearner:x:1000:1000::/home/learner:/bin/bash

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

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

选项-x表示整行匹配:

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

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

[root@centos7 temp]# grep here fileherethere[root@centos7 temp]# grep -w here filehere[root@centos7 temp]#

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

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

选项-n表示显示行号:

[root@centos7 temp]# grep -n "^[r,l]" /etc/passwd1:root:x:0:0:root:/root:/bin/bash5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin24: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/passwdoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologin[root@centos7 temp]# grep -B2 ^operator /etc/passwd halt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin[root@centos7 temp]# grep -C1 ^operator /etc/passwd mail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin

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

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

命令无输出

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

[root@centos7 temp]# egrep "^root|^learner" /etc/passwdroot:x:0:0:root:/root:/bin/bashlearner: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强大的过滤能力来自于各种选项以及正则表达式的配合,在今后的文章中还有更多的例子。

感谢各位的阅读,以上就是“linux怎么对文本或输出内容进行过滤”的内容了,经过本文的学习后,相信大家对linux怎么对文本或输出内容进行过滤这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: linux怎么对文本或输出内容进行过滤

本文链接: https://www.lsjlt.com/news/256875.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作