偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

我們一起玩轉(zhuǎn) Grep 指令

系統(tǒng) Linux
grep這個(gè)linux指令大家一定不陌生,其用于查找文件中符合條件的字符串,下面來(lái)看看這個(gè)高頻的指令如何使用。

在一個(gè)陽(yáng)光明媚、晴空萬(wàn)里的中午,一個(gè)撓頭的程序員正在與團(tuán)隊(duì)一姐排查超時(shí)問(wèn)題,只見(jiàn)一姐手速極快的查找著一個(gè)又一個(gè)日志,快速定位到一個(gè)又一個(gè)嫌疑人,仰慕之情油然而生,為了后續(xù)也能夠在小迷妹手上秀技術(shù),所以暗下決心準(zhǔn)備學(xué)習(xí)這個(gè)牛逼的東西。下面有請(qǐng)今天的主角(grep指令)閃亮登場(chǎng)。

一、基本語(yǔ)法

grep這個(gè)linux指令大家一定不陌生,其用于查找文件中符合條件的字符串,下面來(lái)看看這個(gè)高頻的指令如何使用。

  1. grep [選項(xiàng)] 查找內(nèi)容 [源文件] 

觀察其組成結(jié)構(gòu),由四部分組成:指令名(grep)、選項(xiàng)、查找內(nèi)容、源文件,其中需要注意的有兩個(gè)位置,下面讓我們徐徐道來(lái)。

源文件

源文件部分是可有可無(wú)的,若不指定任何文件名稱或是所給予的文件名為-,則grep指令會(huì)從標(biāo)準(zhǔn)輸入設(shè)備讀取數(shù)據(jù),其使用如下所示:

  1. // 文件路徑為/test 
  2.  
  3. // 接收cat的輸入 
  4. cat ./test |grep 'hello' 
  5.  
  6. // 存在路徑部分參數(shù) 
  7. grep 'hello' ./test 

選項(xiàng)部分

選項(xiàng)部分比較多,可以通過(guò)grep --help指令來(lái)看一下有哪些選項(xiàng):

  1. Regexp selection and interpretation: // 正則表達(dá)式選擇和解釋 
  2.   -E, --extended-regexp     PATTERN is an extended regular expression (ERE) 
  3.   -F, --fixed-strings       PATTERN is a set of newline-separated strings 
  4.   -G, --basic-regexp        PATTERN is a basic regular expression (BRE) 
  5.   -P, --perl-regexp         PATTERN is a Perl regular expression 
  6.   -e, --regexp=PATTERN      use PATTERN for matching 
  7.   -f, --file=FILE           obtain PATTERN from FILE 
  8.   -i, --ignore-case         ignore case distinctions 
  9.   -w, --word-regexp         force PATTERN to match only whole words 
  10.   -x, --line-regexp         force PATTERN to match only whole lines 
  11.   -z, --null-data           a data line ends in 0 byte, not newline 
  12.  
  13. Miscellaneous: // 各種各樣的 
  14.   -s, --no-messages         suppress error messages 
  15.   -v, --invert-match        select non-matching lines // 搜索不匹配的行 
  16.   -V, --version             display version information and exit 
  17.       --help                display this help text and exit 
  18.  
  19. Output control: // 輸出控制 
  20.   -m, --max-count=NUM       stop after NUM matches 
  21.   -b, --byte-offset         print the byte offset with output lines 
  22.   -n, --line-number         print line number with output lines 
  23.       --line-buffered       flush output on every line 
  24.   -H, --with-filename       print the file name for each match 
  25.   -h, --no-filename         suppress the file name prefix on output 
  26.       --label=LABEL         use LABEL as the standard input file name prefix 
  27.   -o, --only-matching       show only the part of a line matching PATTERN 
  28.   -q, --quiet, --silent     suppress all normal output 
  29.       --binary-files=TYPE   assume that binary files are TYPE; 
  30.                             TYPE is 'binary''text'or 'without-match' 
  31.   -a, --text                equivalent to --binary-files=text 
  32.   -I                        equivalent to --binary-files=without-match 
  33.   -d, --directories=ACTION  how to handle directories; 
  34.                             ACTION is 'read''recurse'or 'skip' 
  35.   -D, --devices=ACTION      how to handle devices, FIFOs and sockets; 
  36.                             ACTION is 'read' or 'skip' 
  37.   -r, --recursive           like --directories=recurse 
  38.   -R, --dereference-recursive  likewise, but follow all symlinks 
  39.       --include=FILE_PATTERN  search only files that match FILE_PATTERN 
  40.       --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN 
  41.       --exclude-from=FILE   skip files matching any file pattern from FILE 
  42.       --exclude-dir=PATTERN  directories that match PATTERN will be skipped. 
  43.   -L, --files-without-match  print only names of FILEs containing no match 
  44.   -l, --files-with-matches  print only names of FILEs containing matches 
  45.   -c, --count               print only a count of matching lines per FILE 
  46.   -T, --initial-tab         make tabs line up (if needed) 
  47.   -Z, --null                print 0 byte after FILE name 
  48.  
  49. Context control: // 上下文控制 
  50.   -B, --before-context=NUM  print NUM lines of leading context 
  51.   -A, --after-context=NUM   print NUM lines of trailing context 
  52.   -C, --context=NUM         print NUM lines of output context 
  53.   -NUM                      same as --context=NUM 
  54.       --color[=WHEN], 
  55.       --colour[=WHEN]       use markers to highlight the matching strings; 
  56.                             WHEN is 'always''never'or 'auto' 
  57.   -U, --binary              do not strip CR characters at EOL (MSDOS/Windows) 
  58.   -u, --unix-byte-offsets   report offsets as if CRs were not there 

看著選項(xiàng)內(nèi)容真的很多,背起來(lái)著實(shí)不易,幸好文檔中給我們做了分類,只需要記住這些分類是干什么的,然后在需要的時(shí)候從里面進(jìn)行搜索即可快速搜尋到所需用法(感覺(jué)看其內(nèi)容必看菜鳥(niǎo)教程上的內(nèi)容容易很多)

(1)當(dāng)需要通過(guò)正則的方式進(jìn)行搜索內(nèi)容時(shí),去"Regexp selection and interpretation"區(qū)塊找選項(xiàng)即可,常用的有:

  1. -E:通過(guò)正則表達(dá)式進(jìn)行搜索 

(2)當(dāng)需要對(duì)輸出的內(nèi)容進(jìn)行控制時(shí),去"Output control"區(qū)塊找選項(xiàng)即可,常用的有如下幾個(gè):

  1. -m 數(shù)量:表征匹配多少次就會(huì)停止 
  2. -n:顯示匹配行及行號(hào) 
  3. -H:打印每一個(gè)匹配的文件名 
  4. -r:能夠遞歸查詢,即可以輸入文件夾查詢 
  5. -c:統(tǒng)計(jì)匹配到行的個(gè)數(shù) 

(3)當(dāng)需要獲取輸出內(nèi)容的上下文進(jìn)行操縱時(shí),去"Context control"區(qū)塊找選項(xiàng)即可,常用的有如下幾個(gè):

  1. -B 數(shù)量、-A 數(shù)量、-C 數(shù)量:分別表征獲取內(nèi)容前、后、前后幾行 
  2. --color:對(duì)輸出的內(nèi)容添加顏色 

(4)除了一些劃分比較理解的選項(xiàng),還有一些選項(xiàng)我個(gè)人認(rèn)為劃分的并不是很合理,但是它們?nèi)匀缓苤匾?,讓我們一起?lái)看看有哪些:

  1. -i:忽略字母大小寫(xiě) 
  2. -v:反向選擇,也就是顯示出沒(méi)有搜索出字符串內(nèi)容的那一行 

二、經(jīng)典用法

上面已經(jīng)將其基本使用做了詳細(xì)的闡述,俗話說(shuō)的好:光說(shuō)不練假把式,光練不說(shuō)真把式,連說(shuō)帶練全把式。既然上面闡述了一通理論的東西,下面我們就來(lái)實(shí)戰(zhàn)幾個(gè)常用場(chǎng)景,將理論付諸于實(shí)踐。在實(shí)戰(zhàn)之前先創(chuàng)建一個(gè)文件,文件名是test,文件內(nèi)容如下所示:

  1. hello world!!! 
  2. dog 
  3. cat 
  4. pig 
  5. big pig 
  6. tiger 
  7. Elephant 

從確定文件中過(guò)濾出包含pig的

  1. $ grep 'pig' ./test 
  2. pig 
  3. big pig 

從包含某一部分內(nèi)容的文件中過(guò)濾包含pig的

  1. $ grep 'pig' ./te* 
  2. pig 
  3. big pig 

從某一文件夾下所有內(nèi)容中過(guò)濾出包含pig的

  1. $ grep -r 'pig' . 
  2. ./test:pig 
  3. ./test:big pig 

從某一文件中過(guò)濾出不包含pig的

  1. $ grep -v 'pig' ./test 
  2. hello world!!! 
  3. dog 
  4. cat 
  5. tiger 
  6. Elephant 

在過(guò)濾文件時(shí)顯示行數(shù)

  1. $ grep -n 'pig' ./test 
  2. 4:pig 
  3. 5:big pig 

匹配出以開(kāi)頭的內(nèi)容(通過(guò)基本正則表達(dá)式匹配即可,基本正則表達(dá)式字符有^$.[]*)

  1. $ grep ^p ./test 
  2. pig 

匹配出包含pig或cat內(nèi)容的行(用到了擴(kuò)展正則表達(dá)式,其在基本正則表達(dá)式基礎(chǔ)上增加了(){}?+|等)

  1. $ grep -E 'pig|cat' ./test 
  2. cat 
  3. pig 
  4. big pig 

匹配出包含hello和world內(nèi)容的行

  1. $ grep 'hello' ./test |grep 'world' 
  2. hello world!!! 

獲取到匹配內(nèi)容‘big pig'的前一行內(nèi)容

  1. $ grep -B1 'big pig' ./test 
  2. pig 
  3. big pig 

獲取匹配到'pig'行的數(shù)量

  1. $ grep -c 'pig' ./test 
  2.  

獲取到的pig行的內(nèi)容高亮顯示

  1. $ grep --color 'pig' ./test 
  2. pig 
  3. big pig 

經(jīng)典用法還有很多,不能再一一進(jìn)行羅列了,只需要知道在過(guò)濾內(nèi)容時(shí)用此技巧能解決80%的問(wèn)題,但這就足夠讓自己成為最亮的那個(gè)崽。

·大家好,我是執(zhí)鳶者,畢業(yè)于華中科技大學(xué),新時(shí)代農(nóng)民工,現(xiàn)在是百度前端研發(fā)工程師,著有《前端百題斬》、數(shù)十篇學(xué)習(xí)思維導(dǎo)圖(go、React、Redux、Vue、Vuex、操作系統(tǒng)、Linux、設(shè)計(jì)模式、js、webpack、nginx)以及大量前端進(jìn)階文章。

本文轉(zhuǎn)載自微信公眾號(hào)「前端點(diǎn)線面」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系前端點(diǎn)線面公眾號(hào)。

 

責(zé)任編輯:武曉燕 來(lái)源: 前端點(diǎn)線面
相關(guān)推薦

2021-12-29 08:27:05

ByteBuffer磁盤(pán)服務(wù)器

2022-09-26 14:25:55

Flowable流程ID

2022-02-22 10:50:19

IDEAGit工具,

2021-03-10 12:43:06

LDR指令函數(shù)

2022-03-31 18:59:43

數(shù)據(jù)庫(kù)InnoDBMySQL

2023-08-10 08:28:46

網(wǎng)絡(luò)編程通信

2021-08-27 07:06:09

DubboDocker技術(shù)

2021-01-12 05:08:49

DHCP協(xié)議模型

2022-10-18 07:33:57

Maven構(gòu)建工具

2023-08-04 08:20:56

DockerfileDocker工具

2023-06-30 08:18:51

敏捷開(kāi)發(fā)模式

2023-09-10 21:42:31

2022-05-24 08:21:16

數(shù)據(jù)安全API

2021-07-28 07:53:20

Github ActiDotnet 應(yīng)用

2021-08-27 07:06:10

IOJava抽象

2022-03-08 17:52:58

TCP格式IP

2024-02-20 21:34:16

循環(huán)GolangGo

2021-11-26 07:00:05

反轉(zhuǎn)整數(shù)數(shù)字

2023-03-26 23:47:32

Go內(nèi)存模型

2021-07-15 07:23:28

Singlefligh設(shè)計(jì)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)