Linux 命令 | 常用命令 grep 详解+实例

grep 命令是 Linux 使用频率非常高的一个命令,不管是在日常终端操作中,还是在编程中都会用到,下面结合实例进行介绍。

一、基本语法

grep [options] PATTERN [FILE...]

常用参数:

-i : 忽略大小写(在许多命令中 -i 都是这个含义);

-v ,--invert-match : 反向显示,显示不包含匹配文本的所有行;

-R,-r,--recursive :递归地读每一目录下的所有文件。和 -d recurse 选项等价;

-o, --only-matching :只显示匹配的行中与 PATTERN 相匹配的部分;

-n, --line-number:在输出的每行前面加上它所在的文件中它的行号;

--color :将匹配的内容以高亮显示,一般 grep 中默认已经加上了该参数;

-A NUM, --after-context=NUM:打印出紧随匹配的行之后的下文 NUM 行;

-B NUM, --before-context=NUM:打印出匹配的行之前的上文 NUM 行;

-C NUM, --context=NUM:打印出匹配的行的上下文前后各 NUM 行;

二、实例

2.1 无参数

在当前目录下,查找包含字符串 “hosts” 的文件,如下所示:

[root@localhost ssh]# grep "hosts" ./*
./ssh_config:#   RhostsRSAAuthentication no
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#

 在查找结果中,先是列出了所在的文件,然后输出字符串 “hosts” 所在的行。默认情况下匹配的内容都高亮显示,比如:这里的“hosts”。

查看下系统中命令的别名,如下所示:

[root@localhost ssh]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ssh]#

其中,命令 grep 等于 grep --color=auto,已默认添加了 --color 参数。

2.2 -r 参数

在当前目录下,递归查找包含字符串“hosts” 的文件,即:查找当前目录以及当前目录下的所有目录,递归查找。如下所示: 

[root@localhost etc]# grep -r "hosts" ./*
./avahi/hosts:# See avahi.hosts(5) for more information on this configuration file!
./cupshelpers/preferreddrivers.xml:    <drivertype name="ghostscript">
./cupshelpers/preferreddrivers.xml:      <attribute name="ppd-product" match=".*Ghostscript"/>
./cupshelpers/preferreddrivers.xml:     <drivertype>ghostscript</drivertype>
./dnsmasq.conf:# from /etc/hosts or DHCP only.
./dnsmasq.conf:# If you don't want dnsmasq to read /etc/hosts, uncomment the
./dnsmasq.conf:#no-hosts
./dnsmasq.conf:# or if you want it to read another file, as well as /etc/hosts, use
./dnsmasq.conf:#addn-hosts=/etc/banner_add_hosts
./dnsmasq.conf:# automatically added to simple names in a hosts-file.
./dnsmasq.conf:#expand-hosts
……
./tcsd.conf:#  on this machine's TCSD by TSP's on non-local hosts (over the internet).
./yum/pluginconf.d/fastestmirror.conf:hostfilepath=timedhosts.txt
[root@localhost etc]#

2.3 -i 参数

查找当前目录下,包含字符串“hosts”的文件,且字符串中的字符不区分大小写,如下所示: 

[root@localhost ssh]# grep -i "hosts" ./*
./ssh_config:# HOSTS tmp
./ssh_config:#   RhostsRSAAuthentication no
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#

在上面的查找结果中,HOSTS、Hosts、hosts 都被查找出来了。

2.4 -o 参数

查找当前目录下,包含字符串“hosts”的文件,且仅显示与“hosts”一样的内容。如下所示:

[root@localhost ssh]# grep -o "hosts" ./*
./ssh_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
[root@localhost ssh]#

其中,显示的匹配内容仅显示了与 “hosts” 相同的内容。

2.5 -n 参数

查找当前目录下,包含字符串“hosts”的文件,并显示匹配内容的行号,如下所示:

[root@localhost ssh]# grep -n "hosts" ./*
./ssh_config:24:#   RhostsRSAAuthentication no
./sshd_config:54:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:56:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:59:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:60:#IgnoreRhosts yes
[root@localhost ssh]# 

在匹配内容前加了行号。 

2.6 -A,-B,-C参数

-A : 打印出紧随匹配的行之后的下文 1 行,如下所示:

[root@localhost ssh]# grep -A 1 "hosts" ./*
./ssh_config:#   RhostsRSAAuthentication no
./ssh_config-#   RSAAuthentication yes
--
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
--
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
./sshd_config-
[root@localhost ssh]#

其中,“--” 表示相邻的匹配组之间会打印 -- 的一行,如果两个匹配组在实际文件中位置紧连着,将不会有 “--” 分隔。

-B:打印出匹配的行之前的上文 2 行,如下所示:

[root@localhost ssh]# grep -B 2 "hosts" ./*
./ssh_config-#   ForwardAgent no
./ssh_config-#   ForwardX11 no
./ssh_config:#   RhostsRSAAuthentication no
--
./sshd_config-#AuthorizedKeysCommandUser nobody
./sshd_config-
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
./sshd_config-#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#

-C:打印出匹配的行的上下文前后各 2 行,如下所示:

[root@localhost ssh]# grep -C 2 "hosts" ./*
./ssh_config-#   ForwardAgent no
./ssh_config-#   ForwardX11 no
./ssh_config:#   RhostsRSAAuthentication no
./ssh_config-#   RSAAuthentication yes
./ssh_config-#   PasswordAuthentication yes
--
./sshd_config-#AuthorizedKeysCommandUser nobody
./sshd_config-
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
./sshd_config-#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
./sshd_config-
./sshd_config-# To disable tunneled clear text passwords, change to no here!
[root@localhost ssh]#

三、总结

grep 是 Linux 终端操作中经常使用的一个命令,通常用于查找哪些文件包含指定的内容。

参考文献:

[1] Linux grep 手册;

  • 8
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值