4 3 用grep在檔案中搜尋文字

2022-02-08 05:09:35 字數 4909 閱讀 5134

命令:grep

用途:搜尋文字

用法: grep match_pattern filename

1.支援多種匹配格式

-e:支援正規表示式,等同egrep

yang@ubuntu:~$ echo "

this is a line.

" | grep -e "

[a-z]\.

"this

isa line.

yang@ubuntu:~$ echo "

this is a line.

" | egrep "

[a-z]\.

"this

is a line.

-i:忽略樣式中的大小寫

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.

" | grep -i "

[a-z]\.

"this

isa line.

this

is another line.

-e:匹配多個樣式

yang@ubuntu:~$ echo -e "this is a line.\nthis is another line." | grep -e "[a-z]\." -e "[a-z]\."

this is a line.

this is another line.

-f:從檔案中讀採樣式,逐行寫下需要匹配的樣式

yang@ubuntu:~$ cat pat_file

[a-z]\.

[a-z]\.

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.

" | grep -f pat_file

this

isa line.

this

is another line.

-v:列印除match_pattern之外的所有行

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.\nthis is the third line.

" | grep -v "

third

"this

isa line.

this

is another line.

2.輸出多種形式

-c:統計包含匹配字串的行個數

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.\nthis is the third line.

" | grep -c "

[a-z]\.

"2

-o:只輸出檔案中匹配到的文字部分

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.\nthis is the third line.

" | grep -o "

[a-z]\."e.

e.

統計包含匹配字串的個數

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.\nthis is the third line.

" | grep -o "

[a-z]\.

" | wc -l

2

-b:輸出行距離起始的位置(-b -o:匹配字串距離起始的位置)

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.\nthis is the third line.

" | grep -b "

[a-z]\.

"0:this

isa line.

38:this

isthe third line.

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.\nthis is the third line.

" | grep -b -o "

[a-z]\."13

:e.59:e.

-n:列印出包含匹配字串所在的行號

yang@ubuntu:~$ echo -e "

this is a line.\nthis is another line.\nthis is the third line.

" | grep -n -e "

[a-z]\.

"1:this

isa line.

3:this

is the third line.

-l:輸出匹配的檔案列表

yang@ubuntu:~/file$ cat a.txt

this

isa line.

yang@ubuntu:~/file$ cat b.txt

this

isa line.

yang@ubuntu:~/file$ grep -l "

[a-z]\.

"a.txt b.txt

b.txt

-l:輸出不匹配的檔案列表

yang@ubuntu:~/file$ grep -l "

[a-z]\.

"a.txt b.txt

a.txt

-a:匹配結果之後的行

-b:匹配結果之前的行

-c:匹配結果之前和之後的行

yang@ubuntu:~/file$ grep -e "

[a-z]\.

" file1.txt -c 1

this

isa line.

this

isa second line.

this

is the third line.

-q 返回是否包含,如果包含返回0,沒有返回非0

yang@ubuntu:~/file$ grep -q -e "

[0-9]\.

"file1.txt

yang@ubuntu:~/file$ echo $?

1yang@ubuntu:~/file$ grep -q -e "

[a-z]\.

"file1.txt

yang@ubuntu:~/file$ echo $?

0

3.支援多種檔案選項

支援多檔案

yang@ubuntu:~$ cat file1.txt

this is a line.

this is a second line.

this is the third line.

yang@ubuntu:~$ cat file2.txt

this is a line.

this is a second line.

this is the third line.

yang@ubuntu:~$ grep "[a-z]\." file1.txt file2.txt

file1.txt:this is a second line.

file2.txt:this is a second line.

-r:遞迴搜尋檔案

yang@ubuntu:~/file$ tree

.├── exclude

│   └── file3.txt

├── file1.txt

└── include

└── file2.log

2 directories, 3

files

yang@ubuntu:~/file$ grep "

[a-z]\.

" . -r

./file1.txt:this

isa second line.

./include/file2.log:this

isa second line.

./exclude/file3.txt:this

is a second line.

--include:只在目錄中遞迴包含的檔案

yang@ubuntu:~/file$ grep "

[a-z]\.

" . -r --include *.log

./include/file2.log:this

is a second line.

--exclude:在目錄中排除的檔案之外遞迴

yang@ubuntu:~/file$ grep "

[a-z]\.

" . -r --exclude *.log

./file1.txt:this

isa second line.

./exclude/file3.txt:this

is a second line.

--excelude-dir:排除目錄

yang@ubuntu:~/file$ grep "

[a-z]\.

" . -r --exclude-dir "

exclude

"./file1.txt:this

isa second line.

./include/file2.log:this

is a second line.

grep在文字中查詢內容

功能 grep系列是linux中使用頻率最高的文字查詢命令。主要功能是在乙個或者多個檔案中查詢特定模式的字串。如果該行有匹配的字串,則輸出整個行的內容。如果沒有匹配的內容,則不輸出任何內容。grep命令不改動原始檔。linux的grep家族包括grep egrep fgrep rgrep。grep可...

grep 搜尋目錄中的檔案

plain view plain copy grep fnr class 以上命令的意思是 在當前目錄下的所有檔案中搜尋 class 並顯示檔名 行號和匹配所在行內容 2.在某目錄下的所有檔案中,按使用正則搜尋 plain view plain copy grep pnr d p 是 perl 正規...

grep 搜尋目錄中的檔案

plain view plain copy grep fnr class 以上命令的意思是 在當前目錄下的所有檔案中搜尋 class 並顯示檔名 行號和匹配所在行內容 2.在某目錄下的所有檔案中,按使用正則搜尋 plain view plain copy grep pnr d p 是 perl 正規...