grep命令

grep全稱爲Global search Regular Expression and Print out the line linux

grep是一個能夠利用「正則表達式」進行「全局搜索」的工具;正則表達式

準備一個測試文件greptest文件,並使用grep命令進行搜索

[root@Server-n93yom ~]# cat greptest
grep 123
test 234
grep ABC
GREP abc
[root@Server-n93yom ~]# grep abc greptest
GREP abc
[root@Server-n93yom ~]#

1.)使用grep -i 能夠不區分大小寫的進行搜索bash

[root@Server-n93yom ~]# grep -i  abc greptest
grep ABC
GREP abc
[root@Server-n93yom ~]#

2.)使用grep -n 能夠顯示行號工具

[root@Server-n93yom ~]# grep -i -n  abc greptest
3:grep ABC
4:GREP abc
[root@Server-n93yom ~]#

3.)使用grep --color 或 --color=auto 能夠高亮顯示,由於在Centos7中,系統默認爲grep配置了別名,因此默認就是高亮的,使用alias查看別名測試

[root@Server-n93yom ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'  //默認就是高亮的
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@Server-n93yom ~]#

4.)grep -n 命令是統計匹配到的行數,abc大小寫都匹配共2行ui

[root@Server-n93yom ~]# grep -i -c  abc greptest
2
[root@Server-n93yom ~]#

5.)grep -o 只打印出匹配的關鍵字,而不打印出,和關鍵字同行的其餘信息spa

[root@Server-n93yom ~]# grep -i -o  abc greptest
ABC
abc

  可是,若是一行中有兩個相同的關鍵字,則會分行輸出,而不會在同一行中輸出code

[root@Server-n93yom ~]# cat greptest
grep 123
test 234
grep ABC
GREP abc  abc
[root@Server-n93yom ~]# grep -i -o -n  abc greptest
3:ABC
4:abc
4:abc
[root@Server-n93yom ~]#

6.)grep -B  B爲before的意思,使用該參數,能夠把搜索到的關鍵字前幾行,也給打印出來; regexp

      grep -A 爲after的意思,該命令能夠把搜索到的關鍵字後幾行給打印出來blog

      grep -C 則是把關鍵字先後的行打印出來

7.)grep - w 精確匹配到的單詞,而不是模糊匹配

[root@Server-n93yom ~]# cat greptest
haha
linux
grep 123
kele
test 234
testkkkkuui
grep ABC
GREP abc  abc
[root@Server-n93yom ~]# grep -i -n  test greptest
5:test 234
6:testkkkkuui
[root@Server-n93yom ~]# grep -i -n -w  test greptest
5:test 234

8.) grep -v 查找不包含關鍵字的行

[root@Server-n93yom ~]# cat greptest
haha
linux
grep 123
kele
test 234
testkkkkuui
grep ABC
GREP abc  abc
[root@Server-n93yom ~]# grep -i -n -v  test greptest
1:haha
2:linux
3:grep 123
4:kele
7:grep ABC
8:GREP abc  abc
[root@Server-n93yom ~]#

9.)grep -e 能夠匹配多個關鍵字,匹配任意一個即打印

[root@Server-n93yom ~]# cat greptest
haha
linux
grep 123
kele
test 234
testkkkkuui
grep ABC
GREP abc  abc
[root@Server-n93yom ~]# grep -i  -e abc -e haha greptest
haha
grep ABC
GREP abc  abc
[root@Server-n93yom ~]#

10.)grep -q 判斷是否匹配到,而不打印出來;須要配合echo $? 使用,若echo $?爲0則匹配的到,若爲1則未匹配到

[root@Server-n93yom ~]# cat greptest
haha
linux
grep 123
kele
test 234
testkkkkuui
grep ABC
GREP abc  abc
[root@Server-n93yom ~]# grep -q haha greptest
[root@Server-n93yom ~]# echo $?
0
[root@Server-n93yom ~]# grep -q haha123 greptest
[root@Server-n93yom ~]# echo $?
1

11.)grep -P 正則表達式匹配

給定一個包含電話號碼列表(一行一個電話號碼)的文本文件 file.txt,寫一個 bash 腳本輸出全部有效的電話號碼。

示例:

假設 file.txt 內容以下:

987-123-4567
123 456 7890
(123) 456-7890
# Read from the file file.txt and output all valid phone numbers to stdout.
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt

12.命令列表

-a 或 --text : 不要忽略二進制的數據。
-A<顯示行數> 或 --after-context=<顯示行數> : 除了顯示符合範本樣式的那一列以外,並顯示該行以後的內容。
-b 或 --byte-offset : 在顯示符合樣式的那一行以前,標示出該行第一個字符的編號。
-B<顯示行數> 或 --before-context=<顯示行數> : 除了顯示符合樣式的那一行以外,並顯示該行以前的內容。
-c 或 --count : 計算符合樣式的列數。
-C<顯示行數> 或 --context=<顯示行數>或-<顯示行數> : 除了顯示符合樣式的那一行以外,並顯示該行以前後的內容。
-d <動做> 或 --directories=<動做> : 當指定要查找的是目錄而非文件時,必須使用這項參數,不然grep指令將回報信息並中止動做。
-e<範本樣式> 或 --regexp=<範本樣式> : 指定字符串作爲查找文件內容的樣式。
-E 或 --extended-regexp : 將樣式爲延伸的普通表示法來使用。
-f<規則文件> 或 --file=<規則文件> : 指定規則文件,其內容含有一個或多個規則樣式,讓grep查找符合規則條件的文件內容,格式爲每行一個規則樣式。
-F 或 --fixed-regexp : 將樣式視爲固定字符串的列表。
-G 或 --basic-regexp : 將樣式視爲普通的表示法來使用。
-h 或 --no-filename : 在顯示符合樣式的那一行以前,不標示該行所屬的文件名稱。
-H 或 --with-filename : 在顯示符合樣式的那一行以前,表示該行所屬的文件名稱。
-i 或 --ignore-case : 忽略字符大小寫的差異。
-l 或 --file-with-matches : 列出文件內容符合指定的樣式的文件名稱。
-L 或 --files-without-match : 列出文件內容不符合指定的樣式的文件名稱。
-n 或 --line-number : 在顯示符合樣式的那一行以前,標示出該行的列數編號。
-o 或 --only-matching : 只顯示匹配PATTERN 部分。
-q 或 --quiet或--silent : 不顯示任何信息。
-r 或 --recursive : 此參數的效果和指定"-d recurse"參數相同。
-s 或 --no-messages : 不顯示錯誤信息。
-v 或 --revert-match : 顯示不包含匹配文本的全部行。
-V 或 --version : 顯示版本信息。
-w 或 --word-regexp : 只顯示全字符合的列。
-x --line-regexp : 只顯示全列符合的列。
-y : 此參數的效果和指定"-i"參數相同。
相關文章
相關標籤/搜索