檔案處理三劍客之grep

2021-10-01 15:31:24 字數 1772 閱讀 4802

grep: global search regular expression and print out the line

作用:文字搜尋工具,根據使用者指定的「模式」對目標文字逐行進行匹配檢查;列印匹配到的行

模式:由正規表示式字元及文字字元所編寫的過濾條件

格式

grep

[options] pattern [file...]

常見命令選項:

–color=auto 對匹配到的文字著色顯示

-m # 匹配#次後停止

-v 顯示不被pattern匹配到的行

-i 忽略字元大小寫

-n 顯示匹配的行號

-c 統計匹配的行數

-o 僅顯示匹配到的字串

-q 靜默模式,不輸出任何資訊

-a # after, 後#行

-b # before, 前#行

-c # context, 前後各#行

-e 實現多個選項間的邏輯or關係,如:grep –e 『cat 』 -e 『dog』 file

-w 匹配整個單詞

-e 使用ere,相當於egrep

-f 相當於fgrep,不支援正規表示式

-f file 根據模式檔案處理

-r 遞迴目錄,但不處理軟鏈結

-r 遞迴目錄,但處理軟鏈結

grep root /etc/passwd

grep

"user" /etc/passwd

grep

'user' /etc/passwd

grep

whoami /etc/passwd

範例:

[root@centos8 ~]

#grep "^\(.*\)\>.*\<\1$" /etc/passwd

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

bash:x:1008:1008::/home/bash:/bin/bash

nologin:x:1011:1011::/home/nologin:/sbin/nologin

[root@centos8 ~]

#grep -e "^(.*)\>.*\<\1$" /etc/passwd

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

bash:x:1008:1008::/home/bash:/bin/bash

nologin:x:1011:1011::/home/nologin:/sbin/nologin

[root@centos8 ~]

#egrep "^(.*)\>.*\<\1$" /etc/passwd

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

bash:x:1008:1008::/home/bash:/bin/bash

nologin:x:1011:1011::/home/nologin:/sbin/nologin

三劍客之GREP

grep基本用法 檢索條件關鍵字工具 grep options pattern file.color 高亮顯示匹配到的字串 v 顯示不能被pattern匹配到的 i 忽略字元大小寫 o 僅顯示匹配到的字串 q 靜默模式,不輸出任何資訊 a after,匹配到的後 行 b before,匹配到的前 行...

三劍客之grep

1 grep linux系統中grep命令是一種強大的文字搜尋工具,它能使用正規表示式搜尋文字,並把匹 配的行列印出來。grep全稱是global regular expression print,表示全域性正規表示式版本,它的使用許可權是所有使用者。2 grep主要引數3 grep實用案例 1 搜...

三劍客之grep

grep i 不區分大小寫 color 高亮顯示 v 被匹配到的不顯示 o 只顯示匹配到字串 元字元 任意長度的任意字元 任意單個字元 匹配範圍內的 匹配範圍外的 正規表示式 regular expression regexp 正規表示式預設情況下工作在貪婪模式下 正規表示式的元字元 匹配任意單個字...