自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

一、基本語法

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

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

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

源文件

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

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

選項部分

選項部分比較多,可以通過grep --help指令來看一下有哪些選項:

  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 

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

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

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

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

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

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

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

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

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

二、經(jīng)典用法

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

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

從確定文件中過濾出包含pig的

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

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

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

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

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

從某一文件中過濾出不包含pig的

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

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

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

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

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

匹配出包含pig或cat內(nèi)容的行(用到了擴展正則表達(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)行羅列了,只需要知道在過濾內(nèi)容時用此技巧能解決80%的問題,但這就足夠讓自己成為最亮的那個崽。

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

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

 

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

2021-12-29 08:27:05

ByteBuffer磁盤服務(wù)器

2022-09-26 14:25:55

Flowable流程ID

2022-02-22 10:50:19

IDEAGit工具,

2021-03-10 12:43:06

LDR指令函數(shù)

2021-08-27 07:06:10

IOJava抽象

2024-02-20 21:34:16

循環(huán)GolangGo

2022-03-08 17:52:58

TCP格式IP

2021-07-28 07:53:20

Github ActiDotnet 應(yīng)用

2023-08-04 08:20:56

DockerfileDocker工具

2021-01-12 05:08:49

DHCP協(xié)議模型

2021-08-27 07:06:09

DubboDocker技術(shù)

2022-03-31 18:59:43

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

2022-05-24 08:21:16

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

2023-08-10 08:28:46

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

2022-10-18 07:33:57

Maven構(gòu)建工具

2023-09-10 21:42:31

2023-06-30 08:18:51

敏捷開發(fā)模式

2022-02-14 07:03:31

網(wǎng)站安全MFA

2022-06-26 09:40:55

Django框架服務(wù)
點贊
收藏

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