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

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

【Linux进阶】UNIX体系结构分解——操作系统,内核,shell

1.什么是操作系统? 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境。我们通常将这种软件称为内核(kerel),因为它相对较小,而且位于环境的核心。  从广义上说,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并使计算机具有自己的特生。这里所说的其他软件包括系统实用程序(system utility)、应用程序、shell以及公用函数库等

swiper实例

大家好,我是燐子,今天给大家带来swiper实例   微信小程序中的 swiper 组件是一种用于创建滑动视图的容器组件,常用于实现图片轮播、广告展示等效果。它通过一系列的子组件 swiper-item 来定义滑动视图的每一个页面。 基本用法   以下是一个简单的 swiper 示例代码:   WXML(页面结构) <swiper autoplay="true" interval="3

Java面试题:通过实例说明内连接、左外连接和右外连接的区别

在 SQL 中,连接(JOIN)用于在多个表之间组合行。最常用的连接类型是内连接(INNER JOIN)、左外连接(LEFT OUTER JOIN)和右外连接(RIGHT OUTER JOIN)。它们的主要区别在于它们如何处理表之间的匹配和不匹配行。下面是每种连接的详细说明和示例。 表示例 假设有两个表:Customers 和 Orders。 Customers CustomerIDCus

十四、观察者模式与访问者模式详解

21.观察者模式 21.1.课程目标 1、 掌握观察者模式和访问者模式的应用场景。 2、 掌握观察者模式在具体业务场景中的应用。 3、 了解访问者模式的双分派。 4、 观察者模式和访问者模式的优、缺点。 21.2.内容定位 1、 有 Swing开发经验的人群更容易理解观察者模式。 2、 访问者模式被称为最复杂的设计模式。 21.3.观察者模式 观 察 者 模 式 ( Obser

【操作系统】信号Signal超详解|捕捉函数

🔥博客主页: 我要成为C++领域大神🎥系列专栏:【C++核心编程】 【计算机网络】 【Linux编程】 【操作系统】 ❤️感谢大家点赞👍收藏⭐评论✍️ 本博客致力于知识分享,与更多的人进行学习交流 ​ 如何触发信号 信号是Linux下的经典技术,一般操作系统利用信号杀死违规进程,典型进程干预手段,信号除了杀死进程外也可以挂起进程 kill -l 查看系统支持的信号

Windows/macOS/Linux 安装 Redis 和 Redis Desktop Manager 可视化工具

本文所有安装都在macOS High Sierra 10.13.4进行,Windows安装相对容易些,Linux安装与macOS类似,文中会做区分讲解 1. Redis安装 1.下载Redis https://redis.io/download 把下载的源码更名为redis-4.0.9-source,我喜欢跟maven、Tomcat放在一起,就放到/Users/zhan/Documents

Jitter Injection详解

一、定义与作用 Jitter Injection,即抖动注入,是一种在通信系统中人为地添加抖动的技术。该技术通过在发送端对数据包进行延迟和抖动调整,以实现对整个通信系统的时延和抖动的控制。其主要作用包括: 改善传输质量:通过调整数据包的时延和抖动,可以有效地降低误码率,提高数据传输的可靠性。均衡网络负载:通过对不同的数据流进行不同程度的抖动注入,可以实现网络资源的合理分配,提高整体传输效率。增