Linux Grep命令简介和示例

grep is a tool for filtering text in Linux systems. We can get a specific text or look for a pattern. grep is a tool used daily operation by Linux administrators. We will look at simple usage types in this tutorial. Grep can be used to find a word inside a folder. Grep name came from /g/r/ep expression.

grep是用于在Linux系统中过滤文本的工具。 我们可以获取特定的文本或寻找模式。 grep是Linux管理员日常使用的工具。 在本教程中,我们将介绍简单的用法类型。 Grep可用于在文件夹中查找单词。 Grep名称来自/g/r/ep表达式。

在Linux中安装Grep (Install Grep in Linux)

We hope and expect that most of the modern Linux distributions have installed grep by default. Here is if not

我们希望并期望大多数现代Linux发行版默认都安装了grep。 如果不是这样

Ubuntu,Debian:(Ubuntu, Debian:)

We will use the apt-get command in order to install grep tool.

我们将使用apt-get命令来安装grep工具。

$ sudo apt-get install grep -y

CentOS,Ubuntu: (CentOS, Ubuntu:)

We will use dnf or yum in order to install grep package.

我们将使用dnfyum来安装grep软件包。

$ sudo dnf install grep -y

OR

要么

$ sudo yum install grep -y

简单的Grep文本文件中的一些文本 (Simple Grep Some Text From a Text File)

We start with very simple usage of grep by just getting some text from a file. This is our text file named test.txt

我们通过从文件中获取一些文本开始非常简单地使用grep。 这是我们的名为test.txt的文本文件

httpd-2.4.23.tar.bz2 
httpd-2.4.23.tar.bz2.asc 
mytext.txt 
newtextfile 
nmap-7.31.tgz 
output2 
percona-release_0.1-4.xenial_all.deb 
PNGGRAD8RGB2.PNG 
PNGGRAD8RGB3.PNG 
PNGGRAD8RGB4.PNG 
pnggrad8rgb.jpg 
pnggrad8rgb.png 
source.zip 
ssh_script 
test.zip 
thumb.png 
thumb.pnggrad8rgb.png 
thumb.thumb.png 
thumb.thumb.pnggrad8rgb.png 
thumb.thumb.thumb.png 
yourcommand

Search for PNG in the file named mytext.txt . We will first provide the PNG the name we want to search and then the file or input where we will search.

在名为mytext.txt的文件中搜索PNG 。 我们将首先为PNG提供我们要搜索的名称,然后为文件或输入提供搜索位置。

$ grep PNG mytext.txt
Simple grep example
Simple grep example
简单的grep示例

grep searches whole file and extracts lines that provide text “PNG”. The matched part will be also highlighted with red or similar colors.

grep搜索整个文件并提取提供文本“ PNG”的行。 匹配的部分也将用红色或类似颜色突出显示。

搜索字符串不区分大小写 (Search Strings Case-Insensitive)

By default grep searches case sensitive which means upper and lower case chars are interpreted as different. If we are looking for a hostname in a file it is not important uppercase and lowercase so we will turn off case sensitivity for grep. We will provide -i option to for case insensitive search.

默认情况下,grep搜索区分大小写,这意味着大写和小写字符被解释为不同。 如果我们要在文件中查找主机名,那么大小写并不重要,因此我们将关闭grep的区分大小写。 对于不区分大小写的搜索,我们将提供-i选项。

$ grep -i Thumb mytext.txt
Search Strings Case-Insensitive
Search Strings Case-Insensitive
搜索字符串不区分大小写

As we see that turning off case sensitivity produces more results for the same text.

如我们所见,关闭区分大小写对于相同的文本会产生更多结果。

用Grep递归搜索 (Search Recursive with Grep)

We have searched only in one file for now. If there are hundreds of files with multilevel hierarchy searching one by one is not feasible. So searching recursively is the best solution for this situation. We will provide the -r option which is the shortcut form of the --recursive.

目前我们仅在一个文件中搜索。 如果有数百个具有多层层次结构的文件,则不可行。 因此,递归搜索是这种情况的最佳解决方案。 我们将提供-r选项,这是--recursive的快捷方式。

$ grep -r This
Search Recursive with Grep
Search Recursive with Grep
用Grep递归搜索

This will search from starting the current path to downward recursively. If we provide a path it will start from there like below. In the following example, we will search This term in the whole Linux root file system.

这将从开始当前路径搜索到递归向下搜索。 如果我们提供路径,它将从下面开始。 在以下示例中,我们将在整个Linux根文件系统中搜索This term。

$ grep -r This  /

用Grep搜索单词 (Search for a Word with Grep)

We have searched up to now for a text. But we may search for a word which is separated by spaces. This will search only the whole word match which is test in this case. We will use the -w option and provide the word we want to search like below.

到目前为止,我们一直在搜索文本。 但是我们可能会搜索一个由空格分隔的单词。 这将仅搜索整个单词匹配,在这种情况下将进行test 。 我们将使用-w选项并提供我们想要搜索的单词,如下所示。

$ grep -w test mytext.txt

搜索生产线起点 (Search For Line Start)

We can look for a line start and line and with the following command. We will use ^ as the line start sign and provide the term we want to match. In the following example, we ware looking lines starting with t letter.

我们可以使用以下命令查找行首和行。 我们将使用^作为行的开始符号,并提供我们要匹配的术语。 在以下示例中,我们注意以t letter开头的行。

$ egrep  ^t  mytext.txt

搜索线端 (Search For Line End)

We are looking for lines that end with gcharacter. We are expecting the line end control character after . If there are spaces it will not work. We will use $ as the end of the line sign and put the characters before this sign.

我们正在寻找以g字符结尾的行。 我们期望g之后的行尾控制字符。 如果有空格,将无法使用。 我们将$用作行符号的结尾,并将字符放在该符号之前。

$ egrep  g$  mytext.txt

Grep中的反向匹配 (Reverse Match in Grep)

We have searched for existing terms up to now but can search for non-existing terms. We can name this negative match or reverse match. We will use the -v option and provide the term we do not want to match. In the following example, we will list lines that do not contain the p letter.

到目前为止,我们一直在搜索现有术语,但可以搜索不存在的术语。 我们可以将其命名为否定匹配或反向匹配。 我们将使用-v选项并提供我们不想匹配的术语。 在下面的示例中,我们将列出不包含p字母的行。

$ grep  -v p  mytext.txt
Search Non-Existing Text Lines in Grep
Search Non-Existing Text Lines in Grep
在Grep中搜索不存在的文本行

We are looking for lines that do not have character. This is called also reverse regex because we list negative of search.

我们正在寻找没有p字符的行。 这也称为反向正则表达式,因为我们列出了搜索的负数。

LEARN MORE  How To Use Find Exec Command In Linux Bash?
了解更多信息如何在Linux Bash中使用Find Exec命令?

在Grep中寻找行号 (Look for Line Number in Grep)

If we are looking at some texts upper and lower lines we can provide the count of lines to be shown. It will show the surrounding lines too. We will use -nC option with the number of neighbor lines we want to list or show. In the following example, we want to show a single line around the match and provide 1 like -nC1 as an option.

如果我们在看某些文本的上下行,我们可以提供要显示的行数。 它也会显示周围的线条。 我们将-nC选项与我们要列出或显示的邻居行数一起使用。 在下面的示例中,我们希望在匹配项周围显示一行,并提供-nC1类的1作为选项。

$ grep -nC1 test.zip mytext.txt
Look for Line Number in Grep
Look for Line Number in Grep
在Grep中寻找行号

仅显示文件名(Show Only File Names)

Normally grep will show filenames and the lines that match. We can list only filenames by using -l option. In the following example, we will list the files that contain the term This.

通常,grep将显示文件名和匹配的行。 我们可以使用-l选项仅列出文件名。 在下面的示例中,我们将列出包含术语This的文件。

$ grep -r 'This' -l
Show Only File Names
Show Only File Names
仅显示文件名

This will list files that have This string. We are searching recursively and listing the names of the files.

这将列出具有此字符串的文件。 我们正在递归搜索并列出文件名。

匹配给定的文件扩展名 (Match For Given File Extensions)

We want to grep in just C header files how can we do this? We must supply the –include parameter with extension.

我们只想在C头文件中grep,我们该怎么做? 我们必须为–include参数提供扩展名。

$ grep -r --include \*.h 'stdio' -l * /lib
Grep For Certain File Extensions
Grep For Certain File Extensions
某些文件扩展名的Grep

Grep的唯一印刷比赛 (Only Print Match of Grep)

We may want to get only the match string. Say we need IP addresses and only need the IP address nothing else. -o option only prints exactly matching strings

我们可能只想获取匹配字符串。 假设我们需要IP地址,仅需要IP地址就可以了。 -o选项仅打印完全匹配的字符串

$ grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /var/log/auth.log
Only Print Match of Grep
Only Print Match of Grep
Grep的唯一印刷比赛

egrep命令(egrep Command)

Egrep command is grep -E version which will extend the functionality of grep and provide regular expression support. Grep has 3 different pattern types. First one is Basic which is known BRE second is Extended known as ERE and the third one is Perl PRCE. Below we will look at some examples of grep regular expression support. Here are some Regular Expression Operators. This is our example text. We named this file as myinput

Egrep命令是grep -E版本,它将扩展grep的功能并提供正则表达式支持。 Grep有3种不同的模式类型。 第一个是称为BRE Basic,第二个是称为ERE Extended,第三个是Perl PRCE 。 下面我们将看一些grep正则表达式支持的示例。 这是一些正则表达式运算符。 这是我们的示例文本。 我们将此文件命名为myinput

pof 
tut 
com 
poftut.com 
poftut

? makes preceding item optional and matched at most once.

? 使前一项为可选项,并且最多匹配一次。

$ egrep "of?" myinput
  • makes preceding item will be matched zero or more time

    使前面的项目匹配零个或多个时间
$ egrep "o*" myinput
  • makes preceding item will be matched one or more time

    使前一项匹配一次或多次
$ egrep "o+" myinput

{n} makes preceding item will be matched exactly at n time

{n}使前一项在n次时完全匹配

$ egrep "m{1}" myinput

{n,} makes preceding item will be matched at least n time

{n,}使前面的项目至少匹配n次

$ egrep "(com){1}" myinput

{,m} makes preceding item will be matched at most m time

{,m}使前一项最多匹配m次

$ egrep "(com){,1}" myinput

{n,m} makes preceding item will be matched at least n at most m time

{n,m}使前面的项最多匹配m至少n次

$ egrep "(com){1,1}" myinput

Grep和运算符 (Grep And Operator)

There is no grep And operator specifically in grep we will simulate it by piping multiple greps together like below.

没有grep,并且在grep中,运算符特别是我们将通过如下图所示将多个抓取管道在一起来模拟它。

$ grep "om" myinput | grep "c"

And logic operation can be defined with.

并且可以定义逻辑运算。

LEARN MORE  How To Grep Text Files With Powershell Grep or Select-String Cmdlet In Windows?
了解更多信息如何在Windows中使用Powershell Grep或Select-String Cmdlet来Grep文本文件?

Grep或运算符 (Grep Or Operator)

Or is a logic operation to find one of the provided items. We will look com or o strings in our file. Or is defined with .

或者是查找提供的项目之一的逻辑操作。 我们将在文件中查找com或o字符串。 或用|定义。

$ grep "com\|o" myinput
Grep Or Operator
Grep Or Operator
Grep或运算符

If we want to use it with egrep with regular expression support pipe | can be used as a regular expression grep operator like below.

如果我们想将它与带有正则表达式支持管道的egrep一起使用| 可以用作正则表达式grep运算符,如下所示。

$ egrep "(com){1,1}|o" myinput
Grep Or Operator
Grep Or Operator
Grep或运算符

为Grep着色以实现彩色输出(Colorizing Grep for Colorful Output)

Our grep may find a lot of results. This is very hard to examine. Here colorizing will make our work easier. To make grep results colorful use –color

我们的grep可能会发现很多结果。 这很难检查。 在这里着色将使我们的工作更加轻松。 要使grep结果丰富多彩,请使用–color

$ grep --color t mytext.txt

排除不需要的字符串 (Exclude Unwanted String)

We can remove an unwanted string from our search by piping two greps like below. First, grep removes the unwanted string. Second, grep is for our normal search.

我们可以通过如下所示的两次抓钩从搜索中删除不需要的字符串。 首先,grep删除不需要的字符串。 其次,grep用于我们的常规搜索。

$ grep -v 'png' mytext.txt | grep 't'

计算字符串出现次数 (Count String Occurrences)

We may count the string occurrence. This will give the total number of the string that matches.

我们可以计算字符串出现的次数。 这将给出匹配的字符串总数。

$ grep -c 'png' mytext.txt
Introduction to Linux Grep Command With Examples Infographic
Introduction to Linux Grep Command With Examples Infographic
带有示例信息图Linux Grep命令简介

翻译自: https://www.poftut.com/introduction-to-linux-grep-command-with-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值