前言:本篇是 Linux 基本操作篇章的內(nèi)容!
筆者使用的環(huán)境是基于騰訊云服務(wù)器:CentOS 7.6 64bit。
學(xué)習(xí)集:
- C++ 入門到入土?。?!學(xué)習(xí)合集
- Linux 從命令到網(wǎng)絡(luò)再到內(nèi)核!學(xué)習(xí)合集
注:find 指令常與 grep 指令在面試中被提及,需讓你回答異同!
目錄索引:
1. 基本語(yǔ)法、功能及使用方式
2. 基本用法示例:過(guò)濾查找內(nèi)容
3.「-v」:反向過(guò)濾掉指定內(nèi)容輸出
4. 其他簡(jiǎn)單可選參數(shù)
4.1 「-i」:不區(qū)分大小寫過(guò)濾查找
4.2 「-j」:順帶輸出行號(hào)
5. 補(bǔ)充說(shuō)明:關(guān)聯(lián)正則表達(dá)式
6. 相關(guān)文章或系列推薦
1. 基本語(yǔ)法、功能及使用方式
1.1 基本語(yǔ)法
基本語(yǔ)法: grep [option] 搜尋字符串 文件
1.2 功能及使用方式
功能:在文件中搜索字符串,將找到的行打印出來(lái)
使用方式(兩種):
- grep 指定字符串 指定文件
- cat 指定文件 | grep 指定字符串【該方式結(jié)合管道使用!】
(使用方式見(jiàn)本文的第二點(diǎn))
2. 基本用法示例:過(guò)濾查找內(nèi)容
測(cè)試用例生成
/* 拷貝數(shù)據(jù)集文件用于測(cè)試,注:若無(wú)該文件集可使用如下指令生成:
count=0; while [ $count -le 100 ]; do echo "hello ${count}"; let count++; done > file.txt
*/
[Mortal@VM-12-16-centos test_findsome]$ cd ~
[Mortal@VM-12-16-centos ~]$ ls
StudyingOrder_Linux test1 test2 test3 test_cp test_findsome test_mkdir test_mv test_txtfile
[Mortal@VM-12-16-centos ~]$ ls test_txtfile
file.txt filetxt.txt main.c
[Mortal@VM-12-16-centos ~]$ cp test_txtfile/file.txt test_findsome/grep_test.txt
[Mortal@VM-12-16-centos ~]$ cd test_findsome/
過(guò)濾查找內(nèi)容:即(模糊)查找包含指定字符串的內(nèi)容!
/* 方式一:grep 指定字符串 指定文件 */
[Mortal@VM-12-16-centos test_findsome]$ grep "0" grep_test.txt
hello 0
hello 10
hello 20
hello 30
hello 40
hello 50
hello 60
hello 70
hello 80
hello 90
hello 100
/* 方式二:cat 指定文件 | grep 指定字符串【該方式結(jié)合管道使用!】 */
[Mortal@VM-12-16-centos test_findsome]$ cat grep_test.txt | grep "0"
hello 0
hello 10
hello 20
hello 30
hello 40
hello 50
hello 60
hello 70
hello 80
hello 90
hello 100
3. 「-v」:反向過(guò)濾掉指定內(nèi)容輸出
-v:是一個(gè)可選項(xiàng),作用:反向選擇,亦即顯示出沒(méi)有 ‘搜尋字符串’ 內(nèi)容的那一行
/* 方式一:grep 指定字符串 指定文件 */
[Mortal@VM-12-16-centos test_findsome]$ grep -v "0" grep_test.txt
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
.../* 數(shù)據(jù)過(guò)多,省略不復(fù)制出來(lái) */
/* 方式二:cat 指定文件 | grep 指定字符串【該方式結(jié)合管道使用!】 */
[Mortal@VM-12-16-centos test_findsome]$ cat grep_test.txt | grep -v "0"
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
.../* 數(shù)據(jù)過(guò)多,省略不復(fù)制出來(lái) */
4. 其他簡(jiǎn)單可選參數(shù)
4.1 「-i」:不區(qū)分大小寫過(guò)濾查找
- grep -i 指定字符串 指定文件
- cat 指定文件 | grep -i 指定字符串【該方式結(jié)合管道使用!】
4.2 「-j」:順帶輸出行號(hào)
- grep -n 指定字符串 指定文件
- cat 指定文件 | grep -n 指定字符串【該方式結(jié)合管道使用!】
5. 補(bǔ)充說(shuō)明:關(guān)聯(lián)正則表達(dá)式
此時(shí),只是簡(jiǎn)單先介紹以上內(nèi)容!后續(xù)會(huì)持續(xù)更新本文!
說(shuō)明:grep 指令現(xiàn)在也支持通配符/正則表達(dá)式等!
例如:cat grep_test.txt | grep -v “hello 9[0-5]”
[Mortal@VM-12-16-centos test_findsome]$ cat grep_test.txt | grep 'hello 9[0-5]'
hello 90
hello 91
hello 92
hello 93
hello 94
hello 95
6. 相關(guān)文章或系列推薦
1. Linux 學(xué)習(xí)目錄合集 ;文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-697218.html
2. Linux :: 【基礎(chǔ)指令篇 :: 查找 / 查詢指令:(1)】:: which 指令 :指定系統(tǒng)文件(指令)查找指令 | 查詢指令的別名
3. Linux :: 文件查找指令【2】:find 指令(重點(diǎn)):用于在文件樹(shù)中查找文件(指定路徑/目錄),并作出相應(yīng)的處理(可能訪問(wèn)磁盤)【隨知識(shí)體系持續(xù)更新】文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-697218.html
到了這里,關(guān)于Linux :: 內(nèi)容過(guò)濾指令【3】:grep 指令【詳解】:在指定文件中過(guò)濾搜索信息、(模糊)查找包含指定字符串的內(nèi)容?。ㄈ纾合到y(tǒng)故障時(shí),查看操作日志信息等情景)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!