[root@www ~]# grep [-A] [-B] [--color=auto] '搜尋字串' filename 選項與參數: -A :後面可加數字,為 after 的意思,除了列出該行外,後續的 n 行也列出來; -B :後面可加數字,為 befer 的意思,除了列出該行外,前面的 n 行也列出來; --color=auto 可將正確的那個擷取資料列出顏色 範例一:用 dmesg 列出核心訊息,再以 grep 找出內含 eth 那行 [root@www ~]# dmesg | grep 'eth' eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10 eth0: Identified 8139 chip type 'RTL-8139C' eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1 eth0: no IPv6 routers present # dmesg 可列出核心產生的訊息!透過 grep 來擷取網路卡相關資訊 (eth) , # 就可發現如上資訊。不過沒有行號與特殊顏色顯示!看看下個範例吧! 範例二:承上題,要將捉到的關鍵字顯色,且加上行號來表示: [root@www ~]# dmesg | grep -n --color=auto 'eth' 247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10 248:eth0: Identified 8139 chip type 'RTL-8139C' 294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1 305:eth0: no IPv6 routers present # 你會發現除了 eth 會有特殊顏色來表示之外,最前面還有行號喔! 範例三:承上題,在關鍵字所在行的前兩行與後三行也一起捉出來顯示 [root@www ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth' |