linux命令grep用法,unix grep命令_Linux / UNIX中的Grep命令

 2023-11-19 阅读 23 评论 0

摘要: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 distributi

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.

linux命令grep用法。 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

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

grep命令根據列查找?Syntax

句法

$ grep "string" file name

OR

要么

$ filename grep "string"

Example:

nohup命令, 范例

$ 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”,而且還打印了出現該字符串的行。

unix 搜索文件?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 -v命令、 例

$ 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" *

linux grep?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.

mkdir命令? 在上面的示例中,我們的搜索結果為我們提供了所需的信息,因為字符串“ 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

輸出量

grep命令,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

shell中grep、 輸出量

使用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反轉其輸出,這意味著與打印匹配的行相反,執行相反的操作并打印與表達式不匹配的所有行。 回到我們的文件,讓我們顯示行號,如圖所示。

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

在Vim編輯器上按ESC,在輸入完整的冒號后輸入

set nu

Next, press Enter

接下來,按Enter

Output

grep -e命令詳解。 輸出量

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

現在,要顯示不包含字符串“ Linux”的行,請運行

$ grep -v "Linux" welcome.txt

Output

輸出量

linux查找文件命令grep、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

umount命令。 輸出量

使用-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命令

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/182994.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息