grep遞歸查找頭文件_Grep命令教程–如何使用遞歸查找在Linux和Unix中搜索文件

grep遞歸查找頭文件

grep stands for Globally Search For Regular Expression and Print out. It is a command line tool used in UNIX and Linux systems to search a specified pattern in a file or group of files.

grep代表全局搜索正則表達式并打印出來 。 它是UNIX和Linux系統中使用的命令行工具,用于搜索文件或文件組中的指定模式。

grep comes with a lot of options which allow us to perform various search-related actions on files. In this article, we'll look at how to use grep with the options available as well as basic regular expressions to search files.

grep帶有許多選項,使我們可以對文件執行各種與搜索相關的操作。 在本文中,我們將研究如何將grep與可用選項以及基本正則表達式一起使用來搜索文件。

如何使用grep (How to use grep)

Without passing any option, grep can be used to search for a pattern in a file or group of files. The syntax is:

在不傳遞任何選項的情況下,可以使用grep搜索文件或文件組中的模式。 語法為:

grep '<text-to-be-searched>' <file/files>

Note that single or double quotes are required around the text if it is more than one word.

請注意,如果文本超過一個單詞,則必須在文本周圍加上單引號或雙引號。

You can also use the wildcard (*) to select all files in a directory.

您也可以使用通配符(*)選擇目錄中的所有文件。

The result of this is the occurences of the pattern (by the line it is found) in the file(s). If there is no match, no output will be printed to the terminal.

結果是文件中出現了模式(通過找到的行)。 如果不匹配,則不會將任何輸出打印到終端。

For example, say we have the following files (called grep.txt):

例如,假設我們有以下文件(稱為grep.txt):

Hello, how are you
I am grep
Nice to meet you

The following grep command will search for all occurences of the word 'you':

以下grep命令將搜索單詞“ you”的所有出現:

grep you grep.txt

The result for this is:

結果是:

Hello, how are you
Nice to meet you

you is expected to have a different color than the other text to easily identify what was searched for.

you應該使用與其他文字不同的顏色,以輕松識別所搜索的內容。

But grep comes with more options which help us achieve more during a search operation. Let's look at nine of them while applying them to the example above.

但是grep帶有更多選項,可幫助我們在搜索操作中實現更多目標。 在將它們應用于上面的示例時,讓我們看一下其中的九個。

grep使用的選項 (Options used with grep)

1. -n (--line-number)-列出行號 (1. -n (--line-number) - list line numbers)

This prints out the matches for the text along with the line numbers. If you look at the result we have above, you'll notice there are no line numbers, just the matches.

這會打印出文本的匹配項以及行號。 如果看上面的結果,您會發現沒有行號,只有匹配項。

grep you grep.txt -n

Result:

結果:

1: Hello, how are you
3: Nice to meet you

2. -c (--count)-打印匹配的行數 (2. -c (--count) - prints the number of lines of matches)

grep you grep.txt -c

Result:

結果:

2

Note that if there was another 'you' on line one, option -c would still print 2. This is because it is concerned with the number of lines where the matches appear, not the number of matches.

請注意,如果第一行上還有另一個“您”,則選項-c仍會打印2。這是因為它與匹配項出現的行數有關,而不是匹配數。

3. -v (--invert-match)-打印與指定模式不匹配的行 (3. -v (--invert-match) - prints the lines that do not match the specified pattern)

grep you grep.txt -v -n

Result:

結果:

2. I am grep

Notice that we also used option -n? Yes, you can apply multiple options in one command.

注意,我們還使用了-n選項。 是的,您可以在一個命令中應用多個選項。

4. -i (--ignore-case)-用于不區分大小寫 (4. -i (--ignore-case) - used for case insensitivity)

# command 1
grep You grep.txt
# command 2
grep YoU grep.txt -i

Results:

結果:

# result 1
# no result
# result 2
Hello, how are you
Nice to meet you

5. -l (--files-with-matches)-打印與模式匹配的文件名 (5. -l (--files-with-matches) - print file names that match a pattern)

# command 1
grep you grep.txt -l
# command 2
grep You grep.txt -i -l

Results:

結果:

# result 1
grep.txt
# result 2
# all files in the current directory that matches
# the text 'You' case insensitively
#### 6. `-w` (--word-regexp) - print matches of the whole word

By default, grep matches strings which contain the specified pattern. This means that grep yo grep.txt will print the same results as grep yo grep.txt because 'yo' can be found in you. Similarly, 'ou'.

默認情況下, grep匹配包含指定模式的字符串。 這意味著grep yo grep.txt將打印與grep yo grep.txt相同的結果,因為可以在您中找到“ yo”。 同樣,“ ou”。

With the option -w, grep ensures that the matches are exactly the same pattern as specified. Example:

使用-w選項, grep確保匹配項與指定的模式完全相同。 例:

grep yo grep.txt -w

Result:

結果:

No result!

沒有結果!

7. -o (--only-matching)-僅打印匹配的圖案 (7. -o (--only-matching) - print only the matched pattern)

By default, grep prints the line where the matched pattern is found. With option -o, only the matched pattern is printed line by line. Example:

默認情況下, grep打印找到匹配模式的行。 使用選項-o ,僅匹配的圖案逐行打印。 例:

grep yo grep.txt -o

Result:

結果:

yo

8. -A (--context)和-B (--before-context)-在(分別)匹配的模式之后和之前打印行 (8. -A (--after-context) and -B (--before-context) - print the lines after and before (respectively) the matched pattern)

grep grep grep.txt -A 1 -B 1

Result:

結果:

Hello, how are you
I am grep
Nice to meet you

This matched pattern is on line 2. -A 1 means one line after the matched line and -B 1 means one line before the matched line.

此匹配的模式在第2行上。- -A 1表示匹配線之后的一行, -B 1表示匹配線之前的一行。

There's also a -C (--context) option which is equal to -A + -B. The value passed to -C would be used for -A and -B.

還有一個-C (--context)選項,它等于-A + -B 。 傳遞給-C的值將用于-A-B

9. -R (--dereference-recursive)-遞歸搜索 (9. -R (--dereference-recursive) - recursive search)

By default, grep cannot search directories. If you try doing so, you'll get an error ("Is a directory"). With option -R, searching files within directories and subdirectories becomes possible. Example:

默認情況下, grep無法搜索目錄。 如果嘗試這樣做,將會收到錯誤消息(“是目錄”)。 使用選項-R ,可以在目錄和子目錄中搜索文件。 例:

grep you .

Result:

結果:

# 'you' matches in a folders
# and files starting from the
# current directory

模式的正則表達式 (Regular expressions for patterns)

grep also allows basic regular expressions for specifying patterns. Two of them are:

grep還允許用于指定模式的基本正則表達式。 其中兩個是:

1. ^pattern一行的開始 (1. ^pattern - start of a line)

This pattern means that the grep will match the strings whose lines begin with the string specified after ^. Example:

此模式意味著grep將匹配其行以^之后指定的字符串開頭的字符串。 例:

grep ^I grep.txt -n

Result:

結果:

2: I

2. pattern$ -行尾 (2. pattern$ - end of a line)

In contrast with ^, $ specifies patterns that will be matched if the line ends with the string before $. Example:

^相反, $指定如果行以$之前的字符串結尾的模式將匹配。 例:

grep you$ grep.txt

Result:

結果:

1: Hello, how are you
3: Nice to meet you

結語 (Wrap up)

grep is a powerful tool for searching files in the terminal. Understanding how to use it gives you the ability to easily find files via the terminal.

grep是用于在終端中搜索文件的強大工具。 了解如何使用它使您能夠通過終端輕松查找文件。

There are more options attached to this tool. You can find with man grep.

此工具附帶了更多選項。 您可以與man grep一起找到。

翻譯自: https://www.freecodecamp.org/news/grep-command-tutorial-how-to-search-for-a-file-in-linux-and-unix/

grep遞歸查找頭文件

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390298.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390298.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390298.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++ 前置聲明

&#xff08;一&#xff09;class的前置聲明 class的前置聲明有兩種。 pre.hclass PreA {}; main.hclass PreA; class Main {};//或者 class Main {class PreA* A; }; (二) struct前置聲明 struct的前置聲明只能用第一種。 &#xff08;三&#xff09; 有typedef的前置聲明 Pr…

2.18 特殊權限set_uid 2.19 特殊權限set_gid 2.20 特殊權限stick_bit 2.21 軟鏈接文件 2.22 硬連接文件...

2019獨角獸企業重金招聘Python工程師標準>>> 特殊權限set_uid set_uid:該權限針對二進制可執行文件&#xff0c;使文件在執行階段具有文件所有者的權限&#xff1b; 通俗一點講就是&#xff0c;普通用戶想要訪問一個沒有其他用戶可執行權限的目錄時&#xff0c;暫時…

345. 反轉字符串中的元音字母

345. 反轉字符串中的元音字母 給你一個字符串 s &#xff0c;僅反轉字符串中的所有元音字母&#xff0c;并返回結果字符串。 元音字母包括 ‘a’、‘e’、‘i’、‘o’、‘u’&#xff0c;且可能以大小寫兩種形式出現。 示例 1&#xff1a; 輸入&#xff1a;s “hello” 輸…

通過制作數字桌面游戲和Web應用程序學習JavaScript

Building 2D games can be a great way to learn JavaScript, especially when working through the basics of complex tabletop game logic.制作2D游戲可能是學習JavaScript的好方法&#xff0c;尤其是在研究復雜的桌面游戲邏輯基礎時。 In this series, I’m going to intr…

【HAVENT原創】Node Express API 通用配置

為什么80%的碼農都做不了架構師&#xff1f;>>> ( 基于 Express 4.x ) 啟動文件 /app.js&#xff1a; var express require(express); var bodyParser require(body-parser); var proxy require(http-proxy-middleware); var path require(path);var index re…

C#使用Json.NET解析Json

本文轉載自 http://xiaosheng.me/2016/10/01/article25/ 最近在 C# 項目中需要使用到 Json 格式的數據&#xff0c;我簡單上網搜索了一下&#xff0c;基本上有兩種操作 Json 數據的方法&#xff1a; 使用 Windows 系統自帶的類使用第三方的包本著“第三方包一定有比系統自帶類優…

現在JavaScript日期–如何在JavaScript中獲取當前日期

Many applications you build will have some sort of a date component, whether its the creation date of a resource, or the timestamp of an activity. 您構建的許多應用程序都將具有某種日期組件&#xff0c;無論是資源的創建日期還是活動的時間戳。 Dealing with date…

233. 數字 1 的個數

給定一個整數 n&#xff0c;計算所有小于等于 n 的非負整數中數字 1 出現的個數。 示例 1&#xff1a; 輸入&#xff1a;n 13 輸出&#xff1a;6 示例 2&#xff1a; 輸入&#xff1a;n 0 輸出&#xff1a;0 解題思路 正確性證明 例如&#xff1a;對于n3015&#xff0c…

Linux串口設置參數

為什么80%的碼農都做不了架構師&#xff1f;>>> 在Linux環境下&#xff0c;串口名從ttyS0開始依次是ttyS1、ttyS2等。在本程序中&#xff0c;使用ttyS0作為通信串口。在打開ttyS0的時候選項 O_NOCTTY 表示不能把本串口當成控制終端&#xff0c;否則用戶的鍵盤輸入信…

STM32F013 十元板

我大拇指般大小。STM32F103C8T6&#xff0c;64K Flash&#xff0c;20K RAM&#xff0c;m3的核。十元&#xff0c;應該是價格極限了吧。 通過USB供電&#xff08;5V&#xff09;&#xff0c;也可以排針3.3V供電。可惜沒有引出5V排針。USB口可以供電和USB通訊&#xff0c;沒有USB…

如何在Python中建立和訓練K最近鄰和K-Means集群ML模型

One of machine learnings most popular applications is in solving classification problems.機器學習最流行的應用之一是解決分類問題。 Classification problems are situations where you have a data set, and you want to classify observations from that data set in…

552. 學生出勤記錄 II

552. 學生出勤記錄 II 可以用字符串表示一個學生的出勤記錄&#xff0c;其中的每個字符用來標記當天的出勤情況&#xff08;缺勤、遲到、到場&#xff09;。記錄中只含下面三種字符&#xff1a; ‘A’&#xff1a;Absent&#xff0c;缺勤 ‘L’&#xff1a;Late&#xff0c;遲…

C/C++中計算函數運行時間

#include<stdio.h> #include<time.h> clock_t start,stop;//clock_t 是clock&#xff08;&#xff09;函數返回變量的類型 double duration;//記錄被測函數的運行時間&#xff0c;以秒為單位 int main() { startclock();//開始計時 MyFunction();//把被測函數加在這…

作為一名前端開發工程師,你必須掌握的WEB模板引擎:Handlebars

為什么需要使用模板引擎&#xff1f; 關于為什么要使用模板引擎&#xff0c;按照我常說的一句話就是&#xff1a;不用重復造輪子了。 簡單來說&#xff0c;模板最本質的作用是“變靜為動”&#xff0c;一切利于這方面的都是優勢&#xff0c;不利于的都是劣勢。要想很好地實現“…

extjs 實用開發指南_如何提出有效問題:針對開發人員的實用指南

extjs 實用開發指南Learning is a journey that never ends. At every point in your career, you will keep learning, re-learning, and un-learning. 學習是一個永無止境的旅程。 在職業生涯的每個階段&#xff0c;您都會不斷學習&#xff0c;重新學習和不學習。 The abil…

LOJ 6270

最近&#xff08;一直&#xff09;有點&#xff08;很&#xff09;蠢 按照區間大小排序做區間包含多少區間的話 只用考慮 左端點比當前左端點小的和右端點比當前右端點大的&#xff0c;因為不可能同時滿足 關于K&#xff0c;就在做到K的時候減一下就好了&#xff0c;一直傻逼在…

Zabbix3.4安裝詳細步驟

Zabbix3.4安裝的詳細步驟一、zabbix介紹現在大多數公司都會用到監控軟件&#xff0c;主流的監控軟件就是Zabbix了&#xff0c;當然還會有Nagios等其他的軟件&#xff1a;zabbix是一個基于WEB界面的提供分布式系統監視以及網絡監視功能的企業級的開源解決方案。zabbix能監視各種…

軟件自學成才到公司要學歷嗎_作為一名自學成才的移動開發人員,我在旅途中學到了什么

軟件自學成才到公司要學歷嗎In this post, Ill share my entire journey about how I became a professional mobile developer.在這篇文章中&#xff0c;我將分享我如何成為一名專業的移動開發人員的整個過程。 I hope that reading about my experience will help you refle…

cs231n---語義分割 物體定位 物體檢測 物體分割

1 語義分割 語義分割是對圖像中每個像素作分類&#xff0c;不區分物體&#xff0c;只關心像素。如下&#xff1a; &#xff08;1&#xff09;完全的卷積網絡架構 處理語義分割問題可以使用下面的模型&#xff1a; 其中我們經過多個卷積層處理&#xff0c;最終輸出體的維度是C*H…

http協議內容

前言&#xff1a; http協議&#xff1a; 對瀏覽器客戶端 和 服務器端 之間數據傳輸的格式規范http1.0&#xff1a;當前瀏覽器客戶端與服務器端建立連接之后&#xff0c; 只能發送一次請求&#xff0c;一次請求之后連接關閉。 http1.1&#xff1a;當前瀏覽器客戶端與服務器端建…