本文共 7855 字,大约阅读时间需要 26 分钟。
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命令的用法。
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 bydevelopers and in production servers for hosting crucial components such as weband database servers. Linux has also made a name for itself in PCs.Beginners looking to experiment with Linux can get started with friendlier linuxdistributions 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
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
输出量
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
输出量
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
通常用于显示字符串,而不管它们是否区分大小写。
To count the total number of lines where the string pattern appears or resides, execute the command below
要计算字符串模式出现或驻留的总行数,请执行以下命令
$ grep -c "Linux" welcome.txt
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显示的行不包含搜索模式。
To number the lines where the string pattern is matched , use the -n
option as shown
要对匹配字符串模式的行进行编号,请使用-n
选项,如图所示
$ grep -n "Linux" welcome.txt
Output
输出量
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!
将不会返回任何结果,因为我们不是在搜索模式,而是在搜索准确的单词!
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
输出量
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
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
输出量
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.
感谢您抽出宝贵时间阅读本教程。 随意尝试这些命令,让我们知道它的运行方式。
翻译自:
unix grep命令
转载地址:http://hlmzd.baihongyu.com/