文本處理工具grep常用命令

grep是一種強大的文本搜索工具,它能使用特定模式匹配(包括正則表達式)搜索文本,並默認輸出匹配行。Unix的grep家族包括grep、egrep和fgrep。

格式用法:grep[選項]…模式[文件]…
在每個文件或標準輸入中搜索模式。
默認情況下,模式是一個基本的正則表達式(BRE)。

正則表達式的選擇和解釋:

-E 模式是一個擴展的正則表達式
-e 模式使用模式進行匹配
-f 從文件中獲取模式
-i 忽略大小寫的區別
-w 強制模式,只匹配完整的單詞

查看文件,以這個文件爲示例

root@zhaocheng ~]# cat filetest
ROOT:x:98:0:ROOT:/ROOT:/usr/local
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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

grep -E 這個是一個擴展的正則表達式,支持匹配多元素,可以通過管道符來連接

[root@zhaocheng ~]# grep -E 'root|ROOT|sync' filetest
ROOT:x:98:0:ROOT:/ROOT:/usr/local
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
operator:x:11:0:operator:/root:/sbin/nologin

而grep -e這是一個標準的匹配,一個e只能匹配一個元素,不支持管道,如果想匹配多個,後面需要再加-e再去匹配

[root@zhaocheng ~]# grep -e 'root' -e 'sync' filetest
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
operator:x:11:0:operator:/root:/sbin/nologin

而這裏還有一個egrep,這個相當於grep的擴充版本,也就是本來grep -e不支持多元素匹配,不支持管道,而egrep支持管道
可以這麼理解egrep(在linux中是grep -E)是擴展的grep

[root@zhaocheng ~]# egrep 'root|sync' filetest
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
operator:x:11:0:operator:/root:/sbin/nologin

grep -f 這個一般是用於排查兩個文件當中有沒有一樣的,重複的行,要是有的話,就將重複的行給輸出出來,這個/etc/passwd也是一個文件,前面的filetest也是一個文件,由於作出對比我將filetest刪除了一部分,這樣的話效果會明顯,一般工作中要是對兩個文件進行排查有沒有相同的,有相同的行就過濾出來,可以使用grep -f

格式 grep -f 文件1 文件2

[root@zhaocheng ~]# grep -f filetest /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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

grep -i 忽略大小寫進行匹配,這個''也可以不加

[root@zhaocheng ~]# grep -i 'root' filetest
ROOT:x:98:0:ROOT:/ROOT:/usr/local
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

如果忽略大小寫,還想多進行匹配的話,可以使用egrep,相當於grep -E,這樣支持管道,進行多行匹配了

[root@zhaocheng ~]# egrep -i 'root|halt' filetest
ROOT:x:98:0:ROOT:/ROOT:/usr/local
root:x:0:0:root:/root:/bin/bash
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin

前面使用的匹配都是匹配的行,而只想去匹配一個單詞,可以使用-w,

[root@zhaocheng ~]# egrep -w 'halt|sync' filetest
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
[root@zhaocheng ~]# egrep -w 'hat|syn' filetest

**其他的一些參數

-s 無消息抑制錯誤消息
-v 反匹配選擇不匹配的行
-V 顯示版本信息並退出
--help 顯示此幫助文本並退出**

-s 不顯示錯誤信息,比如沒有這個文件,加-s就不會輸出錯誤內容

[root@zhaocheng ~]# grep kkkk /etc/shadows
grep: /etc/shadows: No such file or directory
[root@zhaocheng ~]# grep -s kkkk /etc/shadows

-v輸出除了lp之外的所有的數據

[root@zhaocheng ~]# grep -v lp filetest
linuxaweqeeqw
ROOT:x:98:0:ROOT:/ROOT:/usr/local
halt$:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
#sync:x:5:0:sync:/sbin:/bin/sync
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
linuxoooghhrhg

-V輸出grep的版本信息並退出

[root@zhaocheng ~]# grep -V
grep (GNU grep) 2.20
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章