grep命令可以检索文件中包含关键字(可以使用正则)的行,默认区分大小写。

ubuntu@ubuntu:~/test$ cat test.txt
this is linux
this is Linux
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$ grep 'linux' test.txt
this is linux
ubuntu@ubuntu:~/test$ grep 'Mysql' test.txt
this is Mysql
ubuntu@ubuntu:~/test$

  

使用 -c 参数,获取包含关键字的行数

ubuntu@ubuntu:~/test$ grep -c 'is' test.txt
4
ubuntu@ubuntu:~/test$ grep -c 'sql' test.txt
2
ubuntu@ubuntu:~/test$

使用 -n 参数,打印内容的同时,显示所在的行号

ubuntu@ubuntu:~/test$ cat test.txt
this is linux
this is Linux
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$ grep -n 'mysql' test.txt
3:this is mysql
ubuntu@ubuntu:~/test$

  

使用 -i 参数,查找时,不区分大小写

ubuntu@ubuntu:~/test$ grep -i 'mysql' test.txt
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$

  

使用 -v 参数,查找不包含关键字的行(反向查找)

ubuntu@ubuntu:~/test$ cat test.txt
this is linux
this is Linux
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$ grep -v 'Linux' test.txt
this is linux
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$

  

使用 -e 参数,可以同时指定多个筛选条件

ubuntu@ubuntu:~$ cat test.txt
this is a
this is b
three are a and b
ubuntu@ubuntu:~$ grep "a" test.txt | grep "this" #与关系,包含a,并且包含this
this is a
ubuntu@ubuntu:~$ grep -e "a" -e "this" test.txt #或关系,包含a或者包含this
this is a
this is b
three are a and b

要想使用正则表达式,可以使用 -E 参数

shell正则和perl语言的正则类似,基本通用。

ubuntu@ubuntu:~/test$ cat test.txt
this is linux
this is Linux
that are apples
ubuntu@ubuntu:~/test$ grep -E '^that' test.txt #以that开头的行
that are apples
ubuntu@ubuntu:~/test$ grep -E 'Linux$' test.txt #以Linux结尾的行
this is Linux
ubuntu@ubuntu:~/test$ grep -E '.inux' test.txt # '.'表示任意一个字符(不包含空白)
this is linux
this is Linux
ubuntu@ubuntu:~/test$ grep -E 'p*' test.txt # ‘*’表示前面一个字母出现0,1或任意多次
this is linux
this is Linux
that are apples
ubuntu@ubuntu:~/test$ grep -E '.+p.+' test.txt # ‘+’表示前面一个字母出现1或任意多次
that are apples
that are apples
ubuntu@ubuntu:~/test$ grep -E 'p{2}' test.txt # {n}前面的一个字符出现n次
that are apples
ubuntu@ubuntu:~/test$

  还有一些常用的匹配模式,比如 '^$'表示一个空行 ;   '^.$'表示只有一个字符的行  ; 使用 \ 来转义,比如使用\.来匹配一个点   ; [0-9]表示匹配一个数字 ; [a-z]|[A-Z]表示任意一个字母; 使用|表示‘或’  ;

ubuntu@ubuntu:~/test$ echo 'ip is 192.168.1.1' > test.txt
ubuntu@ubuntu:~/test$ grep -E '([1-9][0-9]*\.){3}[1-9][0-9]*' test.txt
ip is 192.168.1.1
ubuntu@ubuntu:~/test$

  

  

shell脚本--内容查找之grep命令的更多相关文章

  1. Linux Shell脚本入门--grep命令详解

    grep简介<摘自鸟哥,并加以整理.> grep (global search regular expression(RE) and print out the line,全面搜索正则表达 ...

  2. shell脚本--文件查找之find命令

    首先是通过文件名称来查找,需要使用一个-name参数. 查询以  .txt结尾的文件,和以 t 开头的文件: ubuntu@ubuntu:~/test$ ls one.txt three.txt tw ...

  3. Linux Shell脚本入门--wget 命令用法详解

    Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...

  4. Linux输入输出重定向和文件查找值grep命令

    Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...

  5. 如何隐藏shell脚本内容

    从事 Linux 开发的同学,经常需要编写 shell 脚本,有时脚本中会涉及到一些敏感内容,比如一些 IP 地址,用户名以及密码等,或者脚本中有一些关键的代码, 所有这些内容你都不想别人阅读或者修改 ...

  6. Linux Shell脚本入门--cut命令

    Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...

  7. shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 示例 ...

  8. [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功  shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...

  9. linux shell编程学习笔记(二) --- grep命令

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

随机推荐

  1. Html body的滚动条禁止与启用

    在写一个在页面中,经验证用户没有登录或session失效时候弹出登录框禁止页面滚动用到今天搞了一个功能,上下左右居中,模仿QQ空间里的样式,把横向和纵向滚动条禁止掉代码如下:<script ty ...

  2. ES5-ES6-ES7_let关键字声明变量

    let命令的介绍 let是ECMAScript6中新增的关键字,用于声明变量.它的用法类似于var var a = 3 let b = 4 let变量的声明 let 命令的特点不允许在同一作用域下声明 ...

  3. UVA127-"Accordian" Patience(模拟)

    Problem UVA127-"Accordian" Patience Accept:3260  Submit:16060 Time Limit: 3000 mSec Proble ...

  4. sum-root-to-leaf-numbers (前序遍历)

    class Solution { public: int sumNumbers(TreeNode *root) { ; if (root == NULL) return sum; return pre ...

  5. MySQL慢查询1- 开启慢查询

    本文章摘自网络,学习之用 一.简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二.参数说明 slow_query_log ...

  6. 接上篇,php生成静态页面,加上页面时间缓存

    <?php require_once(dirname(__FILE__).'/include/config.inc.php'); ?> <?php $dosql->Execut ...

  7. 洛谷 P2678 跳石头

    题目背景 一年一度的"跳石头"比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间 ...

  8. firewalld

    1.查看firewalld防火墙自带的区域名 [root@web ~]# firewall-cmd --get-zones block dmz drop external home internal ...

  9. <数据结构与算法分析>读书笔记--最大子序列和问题的求解

    现在我们将要叙述四个算法来求解早先提出的最大子序列和问题. 第一个算法,它只是穷举式地尝试所有的可能.for循环中的循环变量反映了Java中数组从0开始而不是从1开始这样一个事实.还有,本算法并不计算 ...

  10. <转>jmeter(十四)HTTP请求之content-type

    本博客转载自:http://www.cnblogs.com/dinghanhua/p/5646435.html 个人感觉不错,对jmeter最常用的取样器http请求需要用到的信息头管理器做了很好的解 ...