4.3 用grep在文件中搜索文本

命令:grep

用途:搜索文本

用法: grep match_pattern filename

  1.支持多种匹配格式

-E:支持正则表达式,等同egrep

yang@Ubuntu:~$ echo "this is a line." | grep -E "[a-z]\."
this is a 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 is a 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 is a 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 is a 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 is a line.
38:this is the 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 is a line.
3:this is the third line.

-l:输出匹配的文件列表

yang@Ubuntu:~/file$ cat A.txt
this is a line.
yang@Ubuntu:~/file$ cat B.txt
this is a 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 is a line.
this is a second LINE.
this is the third line.

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

yang@Ubuntu:~/file$ grep -q -e "[0-9]\." file1.txt
yang@Ubuntu:~/file$ echo $?
1
yang@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 is a second LINE.
./include/file2.log:this is a 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 is a 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 is a second LINE.
./include/file2.log:this is a second LINE.
原文地址:https://www.cnblogs.com/yangqionggo/p/2867859.html