1. 目标

本文主要介绍 Linuxgrep命令用于查找文件符合条件字符串正则表达式

2. grep 命令

grep命令基本语法是:

grep [options] pattern [files]

常用参数说明

2.1 示例

备注 示例本文件:text.txt

root@dev:~/linux# cat > text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(1)不区分大小写搜索

通过 -i 选项可以给定文件中搜索不区分大小写字符串例如匹配 “UNIX”、”Unix“、”unix “等词。

root@dev:~/linux# grep -i "UNix"  text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(2)显示匹配次数

找出给定字符串/模式匹配行数

root@dev:~/linux# grep -c "unix" text.txt 
-------------------------------------------------------------------
2

(3)显示与模式匹配的文件名

只需显示包含给定字符串/模式的文件即可

root@dev:~/linux# grep -l "unix" *

或者

root@dev:~/linux# grep -l "unix" text.txt text1.txt text2.txt

输出text.txt

(4)检查文件中的整个字符

默认情况下,grep 会匹配给定字符串/模式,即使该字符串/模式在文件中是子串。grep 的 -w选项使其只匹配整个单词

root@dev:~/linux# grep -w "unix" text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(5)只显示匹配的模式默认情况下,grep 会显示匹配字符串的整行我们可以使用 -o 选项让 grep 只显示匹配的字符串。

root@dev:~/linux# grep -o "unix" text.txt 
-------------------------------------------------------------------
unix
unix
unix
unix
unix

(6)使用 grep -n 输出时显示行号

显示匹配行的文件行号。

root@dev:~/linux# grep -n "unix" text.txt 
-------------------------------------------------------------------
1:unix is great os. unix was developed in Bell labs.
4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(7)反向模式匹配

使用 -v 选项可以显示与指定搜索字符串模式不匹配的行。

root@dev:~/linux# grep -v -n "unix" text.txt 
-------------------------------------------------------------------
2:learn operating system.
3:Unix linux which one you choose.

(8)匹配以字符串开头的行

^ 正则表达式指定一行开头。在 grep 中使用它可以匹配以给定字符串或正则匹配开头的行。

root@dev:~/linux# grep "^unix" text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.

(9)匹配以字符串结尾的行

$ 正则表达式指定一行的结尾。在 grep 中可以使用它来匹配以给定字符串或正则表达式结尾的行。

root@dev:~/linux# grep "labs.$" text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.

(10)使用 -e 选项指定表达式

使用 -e 选项指定表达式,可多次使用。

root@dev:~/linux# grep -e "unix" -e "great" -e "os"  text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(11)-f 文件选项 查找符合规则条件的文件内容格式每行一个规则样式

root@dev:~/linux# cat > pattern.txt
-------------------------------------------------------------------
unix
learn
root@dev:~/linux# grep -f pattern.txt  text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(12)打印文件中的 n 个特定行

-A 打印搜索到的行和结果之后的 n 行,-B 打印搜索到的行和结果之前的 n 行,-C 打印搜索到的行和结果之后及之前的 n 行。

语法

$grep -A[NumberOfLines(n)] [search] [file]  

$grep -B[NumberOfLines(n)] [search] [file]  

$grep -C[NumberOfLines(n)] [search] [file]

-A 示例

root@dev:~/linux# grep -A1 great text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.

root@dev:~/linux# grep -A2 great text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.

-B 示例

root@dev:~/linux# grep -B1 choose text.txt 
-------------------------------------------------------------------
learn operating system.
Unix linux which one you choose.

root@dev:~/linux# grep -B2 choose text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.

-C 示例

root@dev:~/linux# grep -C1 choose text.txt 
-------------------------------------------------------------------
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(13)目录递归搜索

-R 指定目录下所有文件中递归搜索。

语法

grep -R [Search] [directory]

示例

root@dev:~/linux# grep -iR operating /root
-------------------------------------------------------------------
Binary file /root/proxy-agent.v0.6.tar matches
/root/get-docker.sh:                    echo "ERROR: Unsupported operating system 'macOS'"
Binary file /root/frontend.tar matches
/root/linux/text.txt:learn operating system.

(14)-E 正则表达式

root@dev:~/linux# grep -E "^[A-Z][a-z]" text.txt 
-------------------------------------------------------------------
Unix linux which one you choose.

(15)-o 输出文件中匹配到的部分

root@dev:~/linux# grep -o -E "[A-Z]" text.txt 
-------------------------------------------------------------------
B
U
N
L

原文地址:https://blog.csdn.net/qq_59831338/article/details/134723501

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_42116.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注