linux三劍客之grep 快速入門

一、概述:

grep(global search regular expression(RE) and print out the line,全面搜索正則表達式並把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。 

1.1  基本格式  

grep [OPTIONS] PATTERN [FILE...]  // options 是參數

常見參數:   -c 統計  -n 行號  -i 忽略大小  -A  搜素到行的後幾行也顯示 -B  後幾行顯示

-v 反選 ( ps -ef | grep java | grep -v grep )

 

二、基本範例:

grep -c "\n" /etc/passwd  //查看有多少換行符的count

grep -i "root" /etc/ssh/sshd_config   //查看多少root忽略大小寫
grep -n "root" /etc/ssh/sshd_config //查詢並且顯示行號

grep -n -A2 "root" /etc/passwd //查詢root,並且把root所在行的後2行也顯示。

 

三、正則範例

grep -n "^root" /etc/passwd  //開頭等於root

grep -n "bash$" /etc/passwd //結尾等於

grep -n ".root.." /etc/passwd // .表示任意字符

grep -n "^[abcd]" /etc/passwd //開頭匹配括號任意字符

grep -n "^[a-d]" /etc/passwd //開頭匹配a-d之間,即abcd

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章