-a 數字:列出符合條件的行,並將連續列出後續n行
-b 數字:列出符合條件的行,並將連續列出前面n行
-c :統計包含字串的行一共幾行!
-i :忽略大小寫
-n :輸出行號
-v :反向查詢(取反)
[root@localhost ~]
# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
檢視以下兩行
[root@localhost ~]
# grep -a 2 "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
--operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:ftp user:/
var/ftp:/sbin/nologin
標出行數
[root@localhost ~]
# grep -n -a 2 "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:ftp user:/
var/ftp:/sbin/nologin
find 和 grep 的區別find 是在系統中查詢符合條件的檔名,預設是完全匹配,如果需要模糊查詢使用萬用字元。 grep 是在檔案中查詢符合條件的字串,是包含匹配,如果需要精確查詢,需要使用正規表示式,此處正則暫不介紹,高階篇中講解。
注
[root@localhost test]
# touch abc 2abc abf sdfg
[root@localhost test]
# ls
1.txt 2abc 2.txt 3.txt 4.txt abc abf sdfg
[root@localhost test]
# ls ?abc
2abc
[root@localhost test]
# ls *abc
2abc abc
[root@localhost test]
# ls [0-9]*
1.txt 2abc 2.txt 3.txt 4.txt
[root@localhost test]
# ls [^0-9]*
abc abf sdfg
格式:命令1 | 命令2
#將命令1的標準輸出作為命令2的標準輸入
[root@localhost test]
# cat 1.txt
123456
123456
123456
[root@localhost test]
# cat 1.txt | head -n 1
123456
格式:命令 |xargs 命令2
#將命令1的標準輸出當做命令2的執行引數(執行物件),預設逐個處理
[root@localhost ~]
# find /etc -name *.txt | cat
/etc/pki/nssdb/pkcs11.txt
[root@localhost ~]
# find /etc -name *.txt |xargs cat
library=libnsssysinit.so
name=nss internal pkcs #11 module
parameters=configdir='sql:/etc/pki/nssdb' certprefix='' keyprefix='' secmod='secmod.db' flags= updatedir='' updatecertprefix='' updatekeyprefix='' updateid='' updatetokendescription=''
nss=flags=internal,moduledbonly,critical trustorder=75 cipherorder=100 slotparams=(1=
)[root@localhost ~]
#
shell grep字元查詢,新增命令
例項一 在當前目錄下test.html中查詢以 pdf 結尾的行,並將結果輸出到test.txt檔案中 more test.html grep pdf test.txt 例項二 參考 1 顯示當前目錄下test.txt所有行 2 顯示test.txt中以 c 開頭的行 more test.txt g...
shell grep命令及常見用法
背景 grep的全稱是global regular expression print,是linux中最強大的文字搜尋命令之一,常用於搜尋文字檔案中是否含有某些特定模式的字串。該命令以行為單位讀取文字並使用正規表示式進行匹配,匹配成功後列印出該行文字。命令格式 grep option string t...
Linux 命令萬用字元的含義
linux萬用字元含義 當前目錄 萬用字元,代表任意0個或多個字元 萬用字元,代表重複0個或乙個0前面的字元 連續不同命令的分隔符 配置檔案注釋 管道 使用者的家目錄 變數前需加的符號 路徑分隔符 或1 重定向,覆蓋 追加重定向,追加 輸入重定向 追加輸入重定向 單引號,不具有變數置換功能,輸出時所...