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

linux基礎命令介紹五:文本過濾 grep

系統(tǒng) Linux
在linux中經(jīng)常需要對文本或輸出內(nèi)容進行過濾,最常用的過濾命令是grep。grep按行檢索輸入的每一行,如果輸入行包含模式PATTERN,則輸出這一行。這里的PATTERN是正則表達式(參考前一篇,本文將結(jié)合grep一同舉例)。

在linux中經(jīng)常需要對文本或輸出內(nèi)容進行過濾,最常用的過濾命令是grep

[[178339]] 

  1. grep [OPTIONS] PATTERN [FILE...] 

grep按行檢索輸入的每一行,如果輸入行包含模式PATTERN,則輸出這一行。這里的PATTERN是正則表達式(參考前一篇,本文將結(jié)合grep一同舉例)。

輸出文件/etc/passwd中包含root的行:

  1. [root@centos7 temp]# grep root /etc/passwd 
  2. root:x:0:0:root:/root:/bin/bash 
  3. operator:x:11:0:operator:/root:/sbin/nologin  

或者從標準輸入獲得:

  1. [root@centos7 temp]# cat /etc/passwd | grep root 
  2. root:x:0:0:root:/root:/bin/bash 
  3. operator:x:11:0:operator:/root:/sbin/nologin  

需要注意的地方是:當grep的輸入既來自文件也來自標準輸入時,grep將忽略標準輸入的內(nèi)容不做處理,除非使用符號-來代表標準輸入:

  1. [root@centos7 temp]# cat /etc/passwd | grep root /etc/passwd - 
  2. /etc/passwd:root:x:0:0:root:/root:/bin/bash 
  3. /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin 
  4. (標準輸入):root:x:0:0:root:/root:/bin/bash
  5. (標準輸入):operator:x:11:0:operator:/root:/sbin/nologin 

此時,grep會標明哪些結(jié)果來自于文件哪些來自于標準輸入。

輸出文件/etc/passwd和文件/etc/group中以root開頭的行:

  1. [root@centos7 temp]# grep "^root" /etc/passwd /etc/group 
  2. /etc/passwd:root:x:0:0:root:/root:/bin/bash 
  3. /etc/group:root:x:0:  

輸出文件/etc/passwd中以/bin/bash結(jié)尾的行:

  1. [root@centos7 temp]# grep "/bin/bash$" /etc/passwd 
  2. root:x:0:0:root:/root:/bin/bash 
  3. learner:x:1000:1000::/home/learner:/bin/bash  

注意以上兩個例子中PATTERN被雙引號引用起來以防止被shell解析。

輸出文件/etc/passwd中不以a-s中任何一個字母開頭的行:

  1. [root@centos7 temp]# grep "^[^a-s]" /etc/passwd  
  2. tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin 
  3. tcpdump:x:72:72::/:/sbin/nologin  

這里需要理解兩個^間不同的含義,***個^表示行首,第二個在[]內(nèi)部的***字符^表示取反。

輸出文件/etc/passwd中字符0連續(xù)出現(xiàn)3次及以上的行(注意轉(zhuǎn)義字符'\'):

  1. [root@centos7 temp]# grep "0\{3,\}" /etc/passwd 
  2. learner:x:1000:1000::/home/learner:/bin/bash  

如輸出文件/etc/passwd中以字符r或l開頭的行:

  1. [root@centos7 temp]# grep "^[r,l]" /etc/passwd 
  2. root:x:0:0:root:/root:/bin/bash 
  3. lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
  4. learner:x:1000:1000::/home/learner:/bin/bash  

選項-i使grep在匹配模式時忽略大小寫:

  1. [root@centos7 temp]# grep -i abcd file  
  2. ABCD 
  3. function abcd() { 
  4. [root@centos7 temp]#  

選項-o表示只輸出匹配的字符,而不是整行:

  1. [root@centos7 temp]# grep -oi abcd file  
  2. ABCD 
  3. abcd 
  4. [root@centos7 temp]#  

選項-c統(tǒng)計匹配的行數(shù):

  1. [root@centos7 temp]# grep -oic abcd file  
  2. [root@centos7 temp]#  

選項-v表示取反匹配,如輸出/etc/passwd中不以/sbin/nologin結(jié)尾的行:

  1. [root@centos7 temp]# grep -v "/sbin/nologin$" /etc/passwd 
  2. root:x:0:0:root:/root:/bin/bash 
  3. sync:x:5:0:sync:/sbin:/bin/sync 
  4. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 
  5. halt:x:7:0:halt:/sbin:/sbin/halt 
  6. learner:x:1000:1000::/home/learner:/bin/bash  

選項-f FILE表示以文件FILE中的每一行作為模式匹配:

  1. [root@centos7 temp]# cat test 
  2. abcd 
  3. ABCD 
  4. [root@centos7 temp]# grep -f test file  
  5. ABCD 
  6. function abcd() { 
  7. [root@centos7 temp]#   

選項-x表示整行匹配:

  1. [root@centos7 temp]# grep -xf test file  
  2. ABCD 
  3. [root@centos7 temp]#  

選項-w表示匹配整個單詞:

  1. [root@centos7 temp]# grep here file 
  2. here 
  3. there 
  4. [root@centos7 temp]# grep -w here file 
  5. here 
  6. [root@centos7 temp]#   

選項-h表示當多個文件時不輸出文件名:

  1. [root@centos7 temp]# cat /etc/passwd|grep ^root - /etc/passwd -h 
  2. root:x:0:0:root:/root:/bin/bash 
  3. root:x:0:0:root:/root:/bin/bash  

選項-n表示顯示行號:

  1. [root@centos7 temp]# grep -n "^[r,l]" /etc/passwd 
  2. 1:root:x:0:0:root:/root:/bin/bash 
  3. 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
  4. 24:learner:x:1000:1000::/home/learner:/bin/bash  

選項-A N、-B N、-C N表示輸出匹配行和其'周圍行'

  1. -A N 表示輸出匹配行和其之后(after)的N行 
  2. -B N 表示輸出匹配行和其之前(before)的N行 
  3. -C N 表示輸出匹配行和其之前之后各N行 
  4. [root@centos7 temp]# grep -A 2 ^operator /etc/passwd 
  5. operator:x:11:0:operator:/root:/sbin/nologin 
  6. games:x:12:100:games:/usr/games:/sbin/nologin 
  7. ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 
  8. [root@centos7 temp]# grep -B2 ^operator /etc/passwd    
  9. halt:x:7:0:halt:/sbin:/sbin/halt 
  10. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
  11. operator:x:11:0:operator:/root:/sbin/nologin 
  12. [root@centos7 temp]# grep -C1 ^operator /etc/passwd   
  13. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
  14. operator:x:11:0:operator:/root:/sbin/nologin 
  15. games:x:12:100:games:/usr/games:/sbin/nologin  

選項-F視PATTERN為它的字面意思匹配(忽略字符的特殊含義),等同于執(zhí)行命令fgrep:

  1. [root@centos7 temp]# grep -F ^root /etc/passwd 
  2. [root@centos7 temp]#   

命令無輸出

選項-E可以使用擴展的正則表達式,如同執(zhí)行egrep命令:

  1. [root@centos7 temp]# egrep "^root|^learner" /etc/passwd 
  2. root:x:0:0:root:/root:/bin/bash 
  3. learner:x:1000:1000::/home/learner:/bin/bash  

使用擴展正則表達式意味著不需要轉(zhuǎn)義就能表示字符的特殊含義,包括?,+,{,|,(和)。

選項-P表示使用perl的正則表達式進行匹配

如:

  1. [root@centos7 ~]# echo "helloworld123456"| grep -oP "\d+" 
  2. 123456 
  3. [root@centos7 ~]#  

perl正則中"\d"表示數(shù)字,+表示匹配一到多次(同vim)。

選項-a將二進制文件當成文本文件處理: 

  1. [root@centos7 ~]# grep -a online /usr/bin/ls 
  2. %s online help: <%s> 
  3. [root@centos7 ~]#  

選項--exclude=GLOB和--include=GLOB分別表示排除和包含匹配GLOB的文件,GLOB表示通配符(find及xargs用法見基礎命令介紹三): 

  1. [root@centos7 temp]# find . -type f | xargs grep --exclude=*.txt --include=test* bash 
  2. ./test.sh:#!/bin/bash 
  3. [root@centos7 temp]#  

grep強大的過濾能力來自于各種選項以及正則表達式的配合,在今后的文章中還有更多的例子。

責任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2012-05-11 10:10:47

Linuxgrep

2016-12-23 13:07:11

shelllinuxsed

2016-12-23 10:56:34

linuxshellawk

2016-11-15 15:50:22

linux基礎命令vim

2010-07-01 14:52:42

Linux grep命

2016-12-08 22:26:28

2009-12-25 13:44:00

grep命令

2017-02-27 14:50:36

Linux命令數(shù)據(jù)

2009-12-11 15:59:00

Linux grep指

2010-03-05 12:50:38

Linux shell

2016-12-27 19:10:38

Linux命令啟動流程

2016-12-29 11:13:30

shellbashlinux

2016-12-27 19:29:14

Linux命令定時任務

2016-12-23 12:46:41

Linux命令進程與內(nèi)存

2016-12-14 19:24:41

2016-12-27 10:34:57

Linux命令軟件包管理

2021-03-29 10:10:15

Linuxgrep

2016-12-27 10:48:59

Linux命令磁盤與文件系統(tǒng)

2020-01-07 09:20:58

Linuxuseradd 命令

2010-03-02 14:35:58

linux文本環(huán)境安裝
點贊
收藏

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