当前位置:首页 > CN2资讯 > 正文内容

linux grep 数量 linux grep -n

2天前CN2资讯


作为Linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很好必要的。
grep家族总共有三个:grep egrep fgrep

常用格式

grep [选项] “模式” [文件]

常用选项

-E:开启扩展(Extend)的正则表达式。
-i:忽略大小写(ignore case)。
-v:反过来(invent),只打印没有匹配的,而匹配的反而不打印。
-n:显示行号。
-w:被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker。
-c:显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项显示有多少行没有被匹配到。
-o:只显示被模式匹配到的字符串。
–color:将匹配到的内容以颜色高亮显示。
-A n:显示匹配到的字符串所咋的行及其后n行,after。
-B n:显示匹配到的字符串所在的行及其前n行,before。
-C n:显示匹配到的字符串所在的行及其前后各n行,context。

示例
[root@localhost etc]# grep "root" /etc/passwd root:x:0:0:root,xisanqi,110,112:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost etc]# grep -i "Root" /etc/passwd root:x:0:0:root,xisanqi,110,112:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost etc]# grep -n "root" /etc/passwd 1:root:x:0:0:root,xisanqi,110,112:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [root@localhost etc]# grep -vc "root" /etc/passwd 27 [root@localhost etc]# grep -o "root" /etc/passwd root root root root [root@localhost ~]# grep -A 2 "vcvxz" /root/bawei.txt vcvxz fdsafas vzxvegf [root@localhost ~]# grep -B 2 "vcvxz" /root/bawei.txt dasdafa fasfaf vcvxz [root@localhost ~]# grep -C 2 "vcvxz" /root/bawei.txt dasdafa fasfaf vcvxz fdsafas vzxvegf

模式部分

1.直接输入要匹配的字符串,可以用fgrep代替来提高查找速度。
2.使用基本正则表达式,下面是基本正则表达式的用法。

匹配字符

.:任意一个字符。
[abc]:表示匹配一个字符,这个字符必须是abc中的一个。
[a-zA-Z]:表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个。
[^123]:匹配一个字符,这个字符使出了1、2、3以外的所有字符。
[a-zA-Z]等价于[[:alpha:]]
[0-9]等价于[[:digit:]]
[a-zA-Z0-9]等价于[[:alnum:]]
tab,space等价于[[:space:]]
[A-Z]等价于[[:upper:]]
[a-z]等价于[[:lower:]]
标点符号 等价于[[:punct:]]

[root@localhost ~]# cat exam.c #include <stdio.h> int main() { String a1="hello"; String a2="world"; String a3="helloworld"; printf("%d\n",a3); return 0; } [root@localhost ~]# grep "hello" exam.c String a1="hello"; String a3="helloworld"; [root@localhost ~]# grep "hello[[:alpha:]]" exam.c String a3="helloworld"; [root@localhost ~]# grep "a[[:digit:]][^[:alpha:]]" exam.c String a1="hello"; String a2="world"; String a3="helloworld"; printf("%d\n",a3);
匹配次数

{m,n}:匹配其前面出现的字符至少m次,至多n次。
?:匹配其前面出现的内容0次或1次,等价于{0,1}。
*:匹配其前面出现的内容任意次,等价于{0,},所以". *"表述任意字符任意次,及无论什么内容全部匹配。

[root@localhost etc]# grep "/.*sh" /etc/passwd root:x:0:0:root,xisanqi,110,112:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin zs100:x:1000:1000::/homes1/zs100:/bin/bash gentoo:x:4004:4004::/users/gentoo:/bin/bash fedora:x:4005:4005::/users/fedora:/bin/bash lisi:x:4006:4006::/homes1/lisi:/bin/bash hu:x:4007:4007::/homes1/hu:/bin/bash zheng:x:4008:4008::/homes1/zheng:/bin/bash zheg:x:4009:4009::/homes1/zheg:/bin/bash [root@localhost etc]# grep "/.\{0,2\}sh" /etc/passwd root:x:0:0:root,xisanqi,110,112:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin zs100:x:1000:1000::/homes1/zs100:/bin/bash gentoo:x:4004:4004::/users/gentoo:/bin/bash fedora:x:4005:4005::/users/fedora:/bin/bash lisi:x:4006:4006::/homes1/lisi:/bin/bash hu:x:4007:4007::/homes1/hu:/bin/bash zheng:x:4008:4008::/homes1/zheng:/bin/bash zheg:x:4009:4009::/homes1/zheg:/bin/bash
位置锚定

^:锚定行首
$ :锚定行尾。技巧:"^$“用于匹配空白行。
\b或<:锚定单词的行首。如”\blike"不会匹配alike,但是会匹配liker。
\b或>:锚定单词的行尾。如"\blike\b"不会撇皮alike和liker,只会匹配like。
\B:与\b作用相反。

[root@localhost etc]# grep "h" /etc/passwd root:x:0:0:root,xisanqi,110,112:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin zs100:x:1000:1000::/homes1/zs100:/bin/bash mariadb:x:4003:5002::/homes1/mariadb:/sbin/nologin gentoo:x:4004:4004::/users/gentoo:/bin/bash fedora:x:4005:4005::/users/fedora:/bin/bash lisi:x:4006:4006::/homes1/lisi:/bin/bash hu:x:4007:4007::/homes1/hu:/bin/bash zheng:x:4008:4008::/homes1/zheng:/bin/bash zheg:x:4009:4009::/homes1/zheg:/bin/bash [root@localhost etc]# grep "h$" /etc/passwd root:x:0:0:root,xisanqi,110,112:/root:/bin/bash zs100:x:1000:1000::/homes1/zs100:/bin/bash gentoo:x:4004:4004::/users/gentoo:/bin/bash fedora:x:4005:4005::/users/fedora:/bin/bash lisi:x:4006:4006::/homes1/lisi:/bin/bash hu:x:4007:4007::/homes1/hu:/bin/bash zheng:x:4008:4008::/homes1/zheng:/bin/bash zheg:x:4009:4009::/homes1/zheg:/bin/bash [root@localhost etc]# grep "^h" /etc/passwd halt:x:7:0:halt:/sbin:/sbin/halt hu:x:4007:4007::/homes1/hu:/bin/bash [root@localhost etc]# grep "sh" /etc/passwd root:x:0:0:root,xisanqi,110,112:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin zs100:x:1000:1000::/homes1/zs100:/bin/bash gentoo:x:4004:4004::/users/gentoo:/bin/bash fedora:x:4005:4005::/users/fedora:/bin/bash lisi:x:4006:4006::/homes1/lisi:/bin/bash hu:x:4007:4007::/homes1/hu:/bin/bash zheng:x:4008:4008::/homes1/zheng:/bin/bash zheg:x:4009:4009::/homes1/zheg:/bin/bash [root@localhost etc]# grep "\<sh" /etc/passwd shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown [root@localhost etc]# grep "\Bsh\b" /etc/passwd root:x:0:0:root,xisanqi,110,112:/root:/bin/bash zs100:x:1000:1000::/homes1/zs100:/bin/bash gentoo:x:4004:4004::/users/gentoo:/bin/bash fedora:x:4005:4005::/users/fedora:/bin/bash lisi:x:4006:4006::/homes1/lisi:/bin/bash hu:x:4007:4007::/homes1/hu:/bin/bash zheng:x:4008:4008::/homes1/zheng:/bin/bash zheg:x:4009:4009::/homes1/zheg:/bin/bash
分组及引用

(string):将string作为一个整体方便后面引用
\1:引用第一个左括号及其对应的右括号所匹配的内容。
\2:引用第二个左括号及其对应的右括号所匹配的内容。
\n:引用第n个左括号及其对应的右括号所匹配的内容。

[root@localhost etc]# grep "^\([[:alpha:]]\).*\1$" /etc/passwd nobody:x:99:99:Nobody:/:/sbin/nologin hu:x:4007:4007::/homes1/hu:/bin/bash

扩展的正则表达式

注意:要使用扩展的正则表达式要加-E选项,或直接使用egrep。

匹配字符

和基本正则表达式一样

匹配次数

*:和基本正则表达式一样
?:基本正则表达式式?,这里没有\。
{m,n}:相比基本正则表达式没有了\。
+:匹配其前面的字符至少一次,相当于{1,}。

位置锚定

和基本正则表达式一样

分组及引用

(string):相比基本正则表达式没有了\。
\1:引用部分和基本正则表达式一样。
\2:引用部分和基本正则表达式一样。

或者

a|b:匹配a或b,注意是a指 | 的左边的整体,b同理。比如 C|cat 表示的是 C或cat,而不是Cat或cat,如果要表示Cat或cat,则应该写为 (C|c)at 。记住(string)除了用于引用还用于分组。

注1:默认情况下,正则表达式的匹配工作在贪婪模式下,也就是说它会尽可能长地去匹配,比如某一行有字符串 abacb,如果搜索内容为 “a.*b” 那么会直接匹配 abacb这个串,而不会只匹配ab或acb。

注2:所有的正则字符,如 [ 、* 、( 等,若要搜索 * ,而不是想把 * 解释为重复先前字符任意次,可以使用 * 来转义。

练习

检索出0-255的范围

[root@localhost network-scripts]# egrep "[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]" /etc/sysconfig/network-scripts/ifcfg-eno16777736 IPV4_FAILURE_FATAL="no" IPV6INIT="yes" IPV6_AUTOCONF="yes" IPV6_DEFROUTE="yes" IPV6_PEERDNS="yes" IPV6_PEERROUTES="yes" IPV6_FAILURE_FATAL="no" NAME="eno16777736" UUID="f29bf5b2-0bb2-40c1-a9c0-acbe48e0dd36" DEVICE="eno16777736"


    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/25799.html

    分享给朋友:

    “linux grep 数量 linux grep -n” 的相关文章

    NameSilo优惠码:轻松节省域名注册与续费费用

    NameSilo优惠码有哪些? NameSilo提供了多种优惠码,帮助用户在注册或续费域名时节省费用。比如,新用户可以使用“NEWUSER10”享受10%的折扣,而“SAVE20”则对所有用户开放,提供20%的折扣。如果你在注册或续费.com域名,可以尝试使用“FREEDOM”优惠码,只需支付99美...

    xTom:灵活可靠的IaaS解决方案,为企业提供优秀网络服务

    xTom是一家成立于2012年的私人控股公司,总部位于德国杜塞尔多夫。它专注于基础设施即服务(IaaS),为各种规模的企业提供可靠的网络和数据中心服务。我对这家公司印象深刻,因为他们提供的解决方案不仅全面,而且非常灵活,能够满足不同客户的需求。 作为一个专业的IaaS提供商,xTom涵盖的服务范围非...

    Bandwagon 意思与效应解析:理解群体行为的心理机制

    “Bandwagon”这个词听上去或许有些陌生,但它的意思和背景却十分有趣。简单来说,Bandwagon指的是一种说服技巧,通常用来引导他人追随某个观点或趋势。你有没有发现,在某些情况下,会有人因为周围大多数人都选择某种方式而随之附和?这种现象正是Bandwagon的核心思想。在这种情况下,个体的决...

    使用Namesilo优惠码注册域名,轻松省钱的秘诀

    Namesilo概述:域名注册的专业选择 要谈到域名注册,我想到的首先就是Namesilo。作为一家成立于2010年的域名注册商,Namesilo吸引了不少用户。我对它的第一印象是专业,尤其是在价格和服务上,使其成为许多人的首选。它的总部位于美国亚利桑那州,作为ICANN认证的注册商,Namesil...

    香港服务器推荐:选择最适合你的服务器类型与服务商

    在数字化时代,服务器的选择对企业和个人用户来说都是一项重要决策。今天,我想和大家聊聊香港服务器。这些服务器以其独特的地理位置和技术优势,成为了许多用户的首选。特别是如果你的目标用户主要在中国大陆和东南亚地区,选择香港服务器无疑是个明智的选择。 首先,香港作为一个国际化大都市,其网络基础设施非常发达。...

    野草云跑路原因分析及用户影响

    在分析野草云跑路的原因时,经济压力与市场环境显得尤为重要。这一行业在过去几年间经历了大量的变化,新的竞争者层出不穷,对业务的生存环境造成了极大的挑战。作为一个处于快速发展的市场,能够获得资金和资源的企业,往往会在这些压力中表现得更为从容。野草云在经济环境受到冲击的情况下,未能及时调整战略,持续的资金...