linux⽂本过滤grep基础命令介绍(5)
在linux中经常需要对⽂本或输出内容进⾏过滤,最常⽤的过滤命令是grep
grep [OPTIONS] PATTERN []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中以字符r或l开头的⾏:
[root@centos7 temp]# grep "^[r,l]" /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 "^[r,l]" /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
选项-F视PATTERN为它的字⾯意思匹配(忽略字符的特殊含义),等同于执⾏命令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表⽰通配符():[root@centos7 temp]# find . -type f | xargs grep --exclude=*.txt --include=test* bash
./test.sh:#!/bin/bash
[root@centos7 temp]#
grep强⼤的过滤能⼒来⾃于各种选项以及正则表达式的配合,在今后的⽂章中还有更多的例⼦。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论