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

2024-05-29 18:18

本文主要是介绍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 手册;

这篇关于Linux 命令 | 常用命令 grep 详解+实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1014281

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客