温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

grep中的正则表达式介绍

发布时间:2021-08-19 09:52:17 来源:亿速云 阅读:113 作者:chen 栏目:建站服务器

本篇内容介绍了“grep中的正则表达式介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

grep是 Linux中用于处理文件的工具之一。grep搜索输入文件,查找与正则表达式匹配的行,并将每个匹配的行标准输出。

正则表达式是匹配一组字符串的模式。模式由操作符、构造文字字符和具有特殊意义的元字符组成。grep支持三种正则表达式语法:Basic、Extended和perl兼容。

如果没有提供正则表达式类型,grep将搜索模式解释为基本的正则表达式。要将模式解释为扩展的正则表达式,请使用-E。

文字匹配

grep 命令最基本的用法是搜索文件中的文字字符或字符序列。例如,要显示/etc/passwd文件中包含字符串“bash”的所有行,需要运行以下 命令:

[root@localhost ~]# grep bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
bob:x:1000:1001::/home/bob:/bin/bash
user01:x:1001:1002::/home/user01:/bin/bash

grep中的正则表达式介绍
默认情况下,grep命令是区分大小写的。这意味着大写和小写字符被视为不同的。要在搜索时忽略大小写,请使用-i选项。

如果搜索字符串包含空格,则需要用单引号或双引号将其括起:

[root@localhost ~]# grep "System message bus" /etc/passwd
dbus:x:81:81:System message bus:/:/sbin/nologin

grep中的正则表达式介绍

锚点

^符号匹配行首的空字符串。在下面的示例中,字符串“root”只有在行首出现时才匹配。

[root@localhost ~]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash

grep中的正则表达式介绍
$要查找以字符串“bash”结尾的行,可以使用以下命令:

[root@localhost ~]# grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bob:x:1000:1001::/home/bob:/bin/bash
user01:x:1001:1002::/home/user01:/bin/bash

grep中的正则表达式介绍
您还可以使用两个锚来构造正则表达式。例如,查看配置文件,不显示空行,请运行以下命令:

[root@localhost ~]# grep -v '^$' /etc/samba/smb.conf

grep中的正则表达式介绍
-v 反转匹配的意义,来选择不匹配的行。

|符号

| 是或者的意思。例如:想查看cpu是否支持虚拟化:

[root@localhost ~]# grep 'vmx\|svm' /proc/cpuinfo 
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities

grep中的正则表达式介绍

https://article.pchome.net/content-2112034.html

https://www.51cto.com/it/news/2020/0525/20939.html

https://www.csdn.net/article/a/2020-05-20/15991626


如果使用扩展正则表达式,则不需要转义|,如下所示:

[root@localhost ~]# grep -E 'svm|vmx' /proc/cpuinfo 
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities

grep中的正则表达式介绍

总结

正则表达式用于文本编辑器、编程语言和命令行工具,如grep、sed和awk。在搜索文本文件、编写 脚本或过滤命令输出时,了解如何构造正则表达式非常有用。

“grep中的正则表达式介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI