24-Ubuntu-文件和目录命令-查找文件内容-grep

grep

  • Linux系统中grep命令是一种强大的文本搜索工具.
  • grep允许文本文件进行模式查找,所谓模式查找,又被称为正则表达式.
选项 含义
-n 显示匹配行及行号
-v 显示不包括匹配文本的所有行(相当于取反)
-i 忽略大小写

 

1.查找所有'hello world'出现的行号

grep -n 'hello world' demo.txt

 

2.大小写查找所有'hello world'出现的行号

grep -ni 'hello world' demo.txt

 

3.查找'hello world'没有出现的行号

grep -vn 'hello world' demo.txt

 

常用的两种模式查找:

参数 含义
^a ^匹配字符串行首,搜寻以a开头的行
ke$ $匹配字符串行尾,搜寻以ke结束的行

 

4.查找以fk开头的行及其行号;以fj结尾的行及其行号(模式查找).

grep -n ^fk demo.txt
grep -n fj$ demo.txt

posted on 2019-06-25 09:19  Summer儿  阅读(2285)  评论(0编辑  收藏  举报

导航