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

相关文章

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤