每天学一点儿shell:Linux三剑客——grep命令
生活随笔
收集整理的這篇文章主要介紹了
每天学一点儿shell:Linux三剑客——grep命令
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 正則表達式
- grep命令用法
- grep命令實例
- 用法一:查找root字符串的行數(shù)以及內(nèi)容
- 用法二:查找"core id"字符串以及前后2行內(nèi)容
- 用法三:匹配包含"sh"字符串的內(nèi)容
- 用法四:匹配以"sh"結(jié)尾前面有0到2個字符的字符串
- 用法五:匹配以"sh"結(jié)尾前面有0到2個字符的英文單詞
- 用法六:查詢以"h"結(jié)尾的字符串
- 用法七:查詢當(dāng)前目錄下的所有文件中包含關(guān)鍵”hello2“ 的文件,以及行數(shù)
前言
Linux的“三劍客”指的是:grep、sed、awk。
之所以被稱為三劍客是通過上述工具可以更好的處理linux的查詢結(jié)果。
正則表達式
上述命令之所以被稱為三劍客是因為他們能很好的結(jié)合正則表達式來處理查詢內(nèi)容,并且只有上述“三劍客”能結(jié)合正則表達式使用。下面介紹以下正則表達式組成字符的含義:
| ^ | 匹配首行 | 表示以某個字符開頭 |
| $ | 匹配行尾 | 表示以某個字符結(jié)尾 |
| ^$ | 空行的意思 | 表示空行的意思 |
| . | 匹配任意單個字符 | 表示匹配任意單個字符 |
| * | 匹配0個或多個此字符 | 表示重復(fù)的任意多個字符 |
| \ | 轉(zhuǎn)義字符 | 表示轉(zhuǎn)義字符 |
| [] | 匹配中括號內(nèi)的字符 | 表示過濾括號內(nèi)的字符 |
| .* | 代表任意多個字符 | 表示匹配任意多個字符 |
特殊的幾個如下:
| . | 任意一個字符。 |
| [abc] | 表示匹配一個字符,這個字符必須是abc中的一個。 |
| [a-zA-Z] | 表示匹配一個字符,這個字符必須是a-z或A-Z這52個字母中的一個。 |
| [^123] | 匹配一個字符,這個字符是除了1、2、3以外的所有字符。 |
對于一些常用的字符集,系統(tǒng)做了定義:
| [A-Za-z] | 等價于 [[:alpha:]] |
| [0-9] | 等價于 [[:digit:]] |
| [A-Za-z0-9] | 等價于 [[:alnum:] |
| tab,space | 等空白字符 [[:space:]] |
| [A-Z] | 等價于 [[:upper:]] |
| [a-z] | 等價于 [[:lower:]] |
| 標點符號 | [[:punct:]] |
匹配次數(shù):
| \{m\} | 匹配其前面出現(xiàn)的字符m次 |
| \{m,\} | 匹配其前面出現(xiàn)的字符至少m次 |
| \{m,n\} | 匹配其前面出現(xiàn)的字符至少m次,至多n次 |
| \? | 匹配其前面出現(xiàn)的內(nèi)容0次或1次,等價于{0,1} |
| * | 匹配其前面出現(xiàn)的內(nèi)容任意次,等價于{0,},所以 “.*” 表述任意字符任意次,即無論什么內(nèi)容全部匹配。 |
三劍客的功能非常強大,但我們只需要掌握他們分別擅長的領(lǐng)域即可:grep擅長查找功能、sed擅長取行和替換、awk擅長取列。
grep命令用法
用法:
grep [選項]... PATTERN [FILE]...列出一些常見的選項命令
| –color | 對匹配到的文本著色顯示 |
| -v | 反過來(invert),顯示不配patern匹配到的行 |
| -i | 忽略大小寫(ignore case) |
| -n | 顯示匹配的行號 |
| -c | 統(tǒng)計匹配的行數(shù) |
| -o | 只顯示被模式匹配到的字符串 |
| -q | 靜默模式,不輸出任何信息 |
| -A | –after-context=NUM 打印以文本結(jié)尾的NUM 行 |
| -B | –before-context=NUM 打印以文本起始的NUM 行 |
| -C | –context=NUM 打印輸出文本NUM 行 |
| -e | 實現(xiàn)多個選項之間的邏輯or關(guān)系,例如:grep -e ‘cat’ -e ‘dog’ file |
| -w | 被匹配的文本只能是單詞,而不能是單詞中的某一部分,如文本中有l(wèi)iker,而我搜尋的只是like,就可以使用-w選項來避免匹配liker |
| -E | 開啟擴展(Extend)的正則表達式,使用正則相當(dāng)于egrep |
| -F | 不支持正則,相當(dāng)與fgrep |
grep命令實例
用法一:查找root字符串的行數(shù)以及內(nèi)容
[root@hadoop-master test-grep]# grep "root" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@hadoop-master test-grep]# grep -n "root" /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [root@hadoop-master test-grep]# grep -vc "root" /etc/passwd 43 [root@hadoop-master test-grep]# grep -o "root" /etc/passwd root root root root用法二:查找"core id"字符串以及前后2行內(nèi)容
[root@hadoop-master test-grep]# grep -A 2 "core id" /proc/cpuinfo core id : 0 cpu cores : 1 apicid : 0 [root@hadoop-master test-grep]# grep -B 2 "core id" /proc/cpuinfo physical id : 0 siblings : 1 core id : 0 [root@hadoop-master test-grep]# grep -C 2 "core id" /proc/cpuinfo physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0用法三:匹配包含"sh"字符串的內(nèi)容
[root@hadoop-master test-grep]# grep "/.*sh" /etc/passwd root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown setroubleshoot:x:990:986::/var/lib/setroubleshoot:/sbin/nologin saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin leo:x:1000:1000:leo:/home/leo:/bin/bash mysql:x:987:1001::/home/mysql:/bin/bash用法四:匹配以"sh"結(jié)尾前面有0到2個字符的字符串
[root@hadoop-master test-grep]# grep "/.\{0,2\}sh" /etc/passwd root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin leo:x:1000:1000:leo:/home/leo:/bin/bash mysql:x:987:1001::/home/mysql:/bin/bash用法五:匹配以"sh"結(jié)尾前面有0到2個字符的英文單詞
[root@hadoop-master test-grep]# grep -w ".\{0,2\}sh" /etc/passwd root:x:0:0:root:/root:/bin/bash leo:x:1000:1000:leo:/home/leo:/bin/bash mysql:x:987:1001::/home/mysql:/bin/bash用法六:查詢以"h"結(jié)尾的字符串
[root@hadoop-master test-grep]# grep "h$" /etc/passwd root:x:0:0:root:/root:/bin/bash leo:x:1000:1000:leo:/home/leo:/bin/bash mysql:x:987:1001::/home/mysql:/bin/bash用法七:查詢當(dāng)前目錄下的所有文件中包含關(guān)鍵”hello2“ 的文件,以及行數(shù)
[root@hadoop-master shell-test]# grep -nr "hello2" ./ ./test-grep/file.txt:leo2 hello2 ./test-grep/file2.text:leo2 hello2 txt2總結(jié)
以上是生活随笔為你收集整理的每天学一点儿shell:Linux三剑客——grep命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5G一些名词解释
- 下一篇: 每天学一点儿shell:Linux三剑客