欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux文本過濾grep基礎(chǔ)命令介紹(5)

 更新時間:2016年12月26日 12:02:18   作者:vvpale  
這篇文章主要為大家詳細介紹了linux文本過濾grep基礎(chǔ)命令,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

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

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

或者從標準輸入獲得:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[root@centos7 temp]# grep -i abcd file 
ABCD
function abcd() {
[root@centos7 temp]#

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

[root@centos7 temp]# grep -oi abcd file 
ABCD
abcd
[root@centos7 temp]#

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

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

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

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

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

[root@centos7 temp]# cat test
abcd
ABCD
[root@centos7 temp]# grep -f test file 
ABCD
function abcd() {
[root@centos7 temp]# 

選項-x表示整行匹配:

[root@centos7 temp]# grep -xf test file 
ABCD
[root@centos7 temp]#

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

[root@centos7 temp]# grep here file
here
there
[root@centos7 temp]# grep -w here file
here
[root@centos7 temp]# 

選項-h表示當(dāng)多個文件時不輸出文件名:

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

選項-n表示顯示行號:

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

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

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

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

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

命令無輸出

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

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

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

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

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

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

選項-a將二進制文件當(dāng)成文本文件處理:

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

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

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • csh,tcsh,bash,sh等shell的區(qū)別

    csh,tcsh,bash,sh等shell的區(qū)別

    這篇文章主要介紹了linux下csh, tcsh,bash, sh等shell的區(qū)別?,需要的朋友可以參考下
    2014-02-02
  • Shell實現(xiàn)系統(tǒng)時間和BIOS時間同步校準腳本分享

    Shell實現(xiàn)系統(tǒng)時間和BIOS時間同步校準腳本分享

    這篇文章主要介紹了Shell實現(xiàn)系統(tǒng)時間和BIOS時間同步校準腳本分享,本文給出了多個時間同步服務(wù)器,然后用ntpdate輪詢同步,需要的朋友可以參考下
    2014-10-10
  • nginx日志切割腳本分享

    nginx日志切割腳本分享

    nginx日志太大怎么辦,只有分割一下了,這篇文章就介紹了一下nginx日志切割腳本,需要的朋友可以參考下
    2014-03-03
  • shell腳本中 /dev/null 的用法小結(jié)

    shell腳本中 /dev/null 的用法小結(jié)

    /dev/null 通常被用于丟棄不需要的輸出流,或作為用于輸入流的空文件,這些操作通常由重定向完成,任何你想丟棄的數(shù)據(jù)都可以寫入其中,本文重點給大家介紹shell腳本中 /dev/null 的用法小結(jié),感興趣的朋友一起看看吧
    2021-09-09
  • pidof命令獲取不到程序的pid問題解決

    pidof命令獲取不到程序的pid問題解決

    這篇文章主要為大家介紹了pidof命令獲取不到程序的pid問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 一篇通俗的Linux Shell體驗教程

    一篇通俗的Linux Shell體驗教程

    Shell是系統(tǒng)的用戶界面,提供了用戶與內(nèi)核進行交互操作的一種接口。它接收用戶輸入的命令并把它送入內(nèi)核去執(zhí)行。本文將簡單介紹Linux shell體驗,感興趣的小伙伴可以參考一下
    2023-05-05
  • CentOS 6.3下給PHP添加mssql擴展模塊教程

    CentOS 6.3下給PHP添加mssql擴展模塊教程

    這篇文章主要介紹了CentOS 6.3下給PHP添加mssql擴展模塊教程,本文使用freetds編譯安裝實現(xiàn),需要的朋友可以參考下
    2014-09-09
  • linux修改文件名的三種方法

    linux修改文件名的三種方法

    我們在使用linux系統(tǒng)過程中為了便于記憶或整理維護,經(jīng)常需要對文件名進行修改,下面文章介紹了linux系統(tǒng)的三種修改文件名稱的方式,需要的朋友可以參考下
    2023-09-09
  • 判斷Linux Shell環(huán)境變量是否存在

    判斷Linux Shell環(huán)境變量是否存在

    這篇文章主要介紹了Linux Shell 判斷環(huán)境變量是否存在的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • shell腳本自動修復(fù)mysql損壞的表

    shell腳本自動修復(fù)mysql損壞的表

    這篇文章主要介紹了shell腳本如何自動修復(fù)mysql損壞的表,需要的朋友可以參考下
    2015-10-10

最新評論