unix grep命令

In Linux and Unix Systems Grep, short for “global regular expression print”, is a command used in searching and matching text files contained in the regular expressions. Furthermore, the command comes pre-installed in every Linux distribution. In this guide, we will look at Common grep command usage with a few examples.

在Linux和Unix系统中,Grep是“全局正则表达式打印”的缩写,是用于搜索和匹配正则表达式中包含的文本文件的命令。 此外,该命令已预安装在每个Linux发行版中。 在本指南中,我们将通过一些示例介绍Common grep命令的用法。

Linux中的Grep命令 (Grep Command in Linux)

Grep command can be used to find or search a regular expression or a string in a text file. To demonstrate this, let’s create a text file welcome.txt and add some content as shown.

Grep命令可用于查找或搜索正则表达式或文本文件中的字符串。 为了演示这一点,让我们创建一个文本文件welcome.txt,并添加一些内容,如图所示。

Welcome to Linux !
Linux is a free and opensource Operating system that is mostly used by
developers and in production servers for hosting crucial components such as web
and database servers. Linux has also made a name for itself in PCs.
Beginners looking to experiment with Linux can get started with friendlier linux
distributions such as Ubuntu, Mint, Fedora and Elementary OS.

Great! Now we are ready to perform a few grep commands and manipulate the output to get the desired results.

大! 现在,我们准备执行一些grep命令并操纵输出以获得所需的结果。

To search for a string in a file, run the command below

要在文件中搜索字符串,请运行以下命令

Syntax

句法

$ grep "string" file name

OR

要么

$ filename grep "string"

Example:

范例

$ grep "Linux" welcome.txt

Output

输出量

As you can see, grep has not only searched and matched the string “Linux” but has also printed the lines in which the string appears.

如您所见,grep不仅搜索并匹配了字符串“ Linux”,而且还打印了出现该字符串的行。

If the file is located in a different file path, be sure to specify the file path as shown below

如果文件位于其他文件路径中,请确保指定文件路径,如下所示

$ grep "string" /path/to/file

使用–color选项为Grep结果着色 (Colorizing Grep results using the –color option)

If you are working on a system that doesn’t display the search string or pattern in a different color from the rest of the text, use the --color to make your results stand out.

如果您正在使用不会以与其余文本不同的颜色显示搜索字符串或模式的系统,请使用--color使结果突出。

Example

$ grep --color "free and opensource" welcome.txt

Output

输出量

在所有目录中递归搜索字符串 (Searching for a string recursively in all directories)

If you wish to search for a string in your current directory and all other subdirectories, search using the - r flag as shown

如果希望在当前目录和所有其他子目录中搜索字符串,请使用- r标志进行搜索,如下所示

$ grep -r "string-name" *

For example

例如

$ grep -r "linux" *

Output

输出量

忽略大小写 (Ignoring case sensitivity)

In the above example, our search results gave us what we wanted because the string “Linux” was specified in Uppercase and also exists in the file in Uppercase. Now let’s try and search for the string in lowercase.

在上面的示例中,我们的搜索结果为我们提供了所需的信息,因为字符串“ Linux”是在大写字母中指定的,并且也存在于大写字母的文件中。 现在,让我们尝试搜索小写的字符串。

$ grep "linux" file name

Nothing from the output, right? This is because grepping could not find and match the string “linux” since the first letter is Lowercase. To ignore case sensitivity, use the -i flag and execute the command below

输出什么都没有,对吧? 这是因为grepping找不到和匹配字符串“ linux”,因为首字母是小写。 要忽略大小写,请使用-i标志并执行以下命令

$ grep -i "linux" welcome.txt

Output

输出量

Awesome isn’t’ it? The - i is normally used to display strings regardless of their case sensitivity.

很棒不是吗? - i通常用于显示字符串,而不管它们是否区分大小写。

用-c选项计算字符串匹配的行 (Count the lines where strings are matched with -c option)

To count the total number of lines where the string pattern appears or resides, execute the command below

要计算字符串模式出现或驻留的总行数,请执行以下命令

$ grep -c "Linux" welcome.txt

Output

输出量

使用Grep反转输出 (Using Grep to invert Output)

To invert the Grep output , use the -v flag. The -v option instructs grep to print all lines that do not contain or match the expression.

要反转Grep输出,请使用-v标志。 -v选项指示grep打印不包含或不匹配表达式的所有行。

The –v option tells grep to invert its output, meaning that instead of printing matching lines, do the opposite and print all of the lines that don’t match the expression. Going back to our file, let us display the line numbers as shown.

–v选项告诉grep反转其输出,这意味着与打印匹配的行相反,执行相反的操作并打印与表达式不匹配的所有行。 回到我们的文件,让我们显示行号,如图所示。

Hit ESC on Vim editor, type a full colon followed by

在Vim编辑器上按ESC,在输入完整的冒号后输入

set nu

Next, press Enter

接下来,按Enter

Output

输出量

Now, to display the lines that don’t contain the string “Linux” run

现在,要显示不包含字符串“ Linux”的行,请运行

$ grep -v "Linux" welcome.txt

Output

输出量

As you can see, grep has displayed the lines that do not contain the search pattern.

如您所见,grep显示的行不包含搜索模式。

用-n选项为包含搜索模式的行编号 (Number the lines that contain the search pattern with -n option)

To number the lines where the string pattern is matched , use the -n option as shown

要对匹配字符串模式的行进行编号,请使用-n选项,如图所示

$ grep -n "Linux" welcome.txt

Output

输出量

使用-w选项搜索完全匹配的单词 (Search for exact matching word using the -w option)

Passing then -w flag will search for the line containing the exact matching word as shown

然后传递-w标志将搜索包含完全匹配的单词的行,如图所示

$ grep -w "opensource" welcome.txt

Output

输出量

However, if you try

但是,如果您尝试

$ grep -w "open" welcome.txt

NO results will be returned because we are not searching for a pattern but an exact word!

将不会返回任何结果,因为我们不是在搜索模式,而是在搜索准确的单词!

在grep中使用管道 (Using pipes with grep)

The grep command can be used together with pipes for getting distinct output.

grep命令可以与管道一起使用以获取不同的输出。

For example, If you want to know if a certain package is installed in Ubuntu system execute

例如,如果您想知道是否在Ubuntu系统中安装了某个软件包,请执行

$ dpkg -L | grep "package-name"

For example, to find out if OpenSSH has been installed in your system pipe the dpkg -l command to grep as shown

例如,要查看系统管道中是否已安装OpenSSH,请使用dpkg -l命令到grep,如下所示

$ dpkg -L | grep -i "openssh"

Output

输出量

在搜索模式之前或之后显示行数使用管道 (Displaying number of lines before or after a search pattern Using pipes )

You can use the -A or -B to dislay number of lines that either precede or come after the search string. The -A flag denotes the lines that come after the search string and -B prints the output that appears before the search string.

您可以使用-A-B来布置搜索字符串之前或之后的行数。 -A标志表示搜索字符串之后的行, -B打印出现在搜索字符串之前的输出。

For example

例如

$ ifconfig | grep -A 4 ens3

This command displays the line containing the string plus 4 lines of text after the ens string in the ifconfig command.

此命令在ifconfig命令中显示包含字符串的行以及ens字符串后的4行文本。

Output

输出量

Conversely, in the example below, the use of the -B flag will display the line containing the search string plus 3 lines of text before the ether string in the ifconfig command.

相反,在下面的示例中,使用-B标志将在ifconfig命令中显示包含搜索字符串的行以及以太字符串之前的3行文本。

Output

输出量

$ ifconfig | grep -B 4 ether

在正则表达式(REGEX)中使用grep (Using grep with regual expressions (REGEX) )

The term REGEX is an acronym for REGular EXpression. A REGEX is a sequence of characters that is used to match a pattern. Below are a few examples:

术语REGEX是REG EX表达式的缩写。 REGEX是用于匹配模式的字符序列。 以下是一些示例:

^      Matches characters at the beginning of a line
$      Matches characters at the end of a line
"."    Matches any character
[a-z]  Matches any characters between A and Z
[^ ..] Matches anything apart from what is contained in the brackets

Example
To print lines beginning with a certain character, the syntax is;


要打印以某个字符开头的行,语法为:

grep ^character file_name

For instance, to display the lines that begin with the letter “d” in our welcome.txt file, we would execute

例如,要在welcome.txt文件中显示以字母“ d”开头的行,我们将执行

$ grep ^d welcome.txt

Output

输出量

To display lines that end with the letter ‘x’ run

显示以字母“ x”结尾的行

$ grep x$ welcome.txt

Output

输出量

获取更多Grep选项的帮助 (Getting help with more Grep options)

If you need to learn more on Grep command usage, run the command below to get a sneak preview of other flags or options that you may use together with the command.

如果您需要了解有关Grep命令用法的更多信息,请运行以下命令以预览可与该命令一起使用的其他标志或选项。

$ grep --help

Sample Output

样本输出

We appreciate your time for going through this tutorial. Feel free to try out the commands and let us know how it went.

感谢您抽出宝贵时间阅读本教程。 随意尝试这些命令,让我们知道它的运行方式。

翻译自: https://www.journaldev.com/24271/grep-command-in-linux-unix

unix grep命令

unix grep命令_Linux / UNIX中的Grep命令相关推荐

  1. 昊鼎王五:Windows运行中的所有命令_Windows快捷命令_运行中的所有命令

    昊鼎王五:Windows运行中的所有命令_Windows快捷命令_"运行"中的所有命令 winver 检查Windows版本 wmimgmt.msc 打开Windows管理体系结构 ...

  2. 在windows的命令窗口cmd中 添加curl命令

    在windows的命令窗口cmd中 添加curl命令 第一步:下载curl.exe 下载地址 http://download.csdn.net/detail/taoshujian/9766612 第二 ...

  3. unix和linux命令_Linux / UNIX中的cp命令

    unix和linux命令 In this guide, we focus on cp command in Linux/Unix systems. cp command – short for cop ...

  4. dos2unix命令找不到_Linux系统中的dos2unix命令

    CentOS7.3学习笔记总结(二十八)-dos2unix命令 使用过linux系统的朋友都知道,linux和windows换行符是不同的,Windows格式文件的换行符为 ,而Unix&Li ...

  5. linux中使用u盘和光驱的命令_Linux文件操作高频使用命令

    0.新建操作: mkdir abc #新建一个文件夹touch abc.sh #新建一个文件 1.查看操作 查看目录: ll #显示目录文件详细信息du -h 文件/目录 #查看大小pwd #显示路径 ...

  6. linux中vim查看最后五行命令,Linux系统中Vi常用命令及高级应用

    一.简介 Vi命令可以说是Unix/Linux世界里最常用的编辑文件的命令了,很多人不喜欢VI因为它 的众多的命令集,但是我们只需要掌握基本的命令然后灵活地加以运用,相信你会象我一样喜欢它的. 本文旨 ...

  7. linux cut命令学习,Linux中的cut 命令详解

    今天小编要跟大家分享的文章是关于Linux中的cut 命令详解.cut 命令在Linux和Unix中的作用是从文件中的每一行中截取出一些部分,并输出到标准输出中.我们可以使用 cut 命令从一行字符串 ...

  8. php redis命令大全,redis中key相关命令详解

    一.概述: 本文将主要讲述与Key相关的Redis命令.学习这些命令对于学习Redis是非常重要的基础,也是能够充分挖掘Redis潜力的利器.(推荐:redis视频教程) 二.相关命令列表: 命令原型 ...

  9. linux基础命令_Linux编程基础:常用命令

    命令格式 在学习具体命令之前,需先了解Linux常用命令的基本格式.Linux系统中的命令遵循如下的基本格式: command [options] [arguments] 其中command表示命令的 ...

最新文章

  1. Building for Production
  2. yolo v3学习笔记
  3. 编写UEditor插件
  4. 开放对静态资源的访问
  5. 「作文素材详解」写作必知篇:语言优美不是作文第一要求
  6. 中秋快乐:数据库的全家福指尖细数识几何?
  7. 2016年Google面筋记录
  8. 【滚动数组】【状压dp】Gym - 100956F - Colored Path
  9. java拦截器跳转页面跳转页面跳转_java Struts2 在拦截器里的跳转问题
  10. 小小方法,问题锦集。
  11. python 关联矩阵_创建关联矩阵
  12. 虚假信息成物联网“毒瘤”
  13. 91地图坐标系矫正教程
  14. qcom usb驱动下载_艾肯Mobile Q驱动-艾肯Mobile Q usb外置声卡驱动下载v1.35.20 官方最新版-西西软件下载...
  15. NTFS 数据流隐写学习
  16. java页面展示_JAVA页面展示问题
  17. SpringSecurity简单集成
  18. Mac技巧:新手必看Macbook快捷键使用大全
  19. MySQL-7 DDL约束 标识列 TCL事务控制语言 视图view
  20. LDdecay计算和做图

热门文章

  1. 一步步学习微软InfoPath2010和SP2010--第十二章节--管理和监控InfoPath Form Services(IPFS)(4)--监控含图片控件的Products表单...
  2. centos 6.3 64bit 安装VMware workstation 9.1 64bit
  3. Guacamole-HTML5无客户端远程桌面
  4. [转载] python实现基本算法之插入排序(Insertion Sort)
  5. MySQL之存储引擎,数据类型,约束条件
  6. fzyzojP1635 -- 平均值
  7. sqlite3 语法
  8. php 进程管理及操作
  9. Autolayout屏幕适配——代码实现(苹果公司 / VFL语言 / 第三方框架Masonry)
  10. OSGi.NET 学习笔记 [模块化和插件化][概念]