Linux命令(1): grep

grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

grep的命令格式为:grep [option] pattern file,其常用选项包括:

Option 说明
-c 只输出匹配的行数
-i 不区分大小写
-h 查询多文件时不显示文件名
-l 查询多文件时只输出包含匹配字符的文件名
-n 显示匹配行及行号

给定文件内容如下:

hello world
hello c++
hi python
hi java

下面的该命令的部分使用示例:

# 搜索显示文件中匹配的行
grep hell* test.txt
# hello world
# hello c++

# 搜索时不区分大小写
grep -i hell* test.txt
# hello world
# hello c++
# Hello golang

# 指输出匹配的行数
grep -c hell* test.txt
# 2
linux
40 views
Comments
登录后评论
Sign In