Linux系统中的grep命令怎么用

发布时间:2022-01-24 10:10:16 作者:iii
来源:亿速云 阅读:164

本篇内容主要讲解“Linux系统中的grep命令怎么用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux系统中的grep命令怎么用”吧!

在Linux系统中grep时最常用的处理文件的的工具之一,grep命令最基本的用法是搜索文件中的文字字符或字符序列

文字匹配

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命令是区分大小写的。这意味着大写和小写字符被视为不同的。要在搜索时忽略大小写,请使用-i选项。

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

[root@localhost ~]# grep “System message bus” /etc/passwd

dbus:x:81:81:System message bus:/:/sbin/nologin

锚点

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

[root@localhost ~]# grep ‘^root’ /etc/passwd

root:x:0:0:root:/root:/bin/bash

$要查找以字符串“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

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

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

-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

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

[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

到此,相信大家对“Linux系统中的grep命令怎么用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. linux中怎样使用grep命令
  2. Linux中的grep命令怎么用

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

grep linux

上一篇:Linux系统中bash命令怎样用

下一篇:Linux系统如何配置DHCP服务器

相关阅读

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

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