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. Git解决冲突(本地共享仓库简单实践)

    1:可以使用git init --bare初始化一个本地共享仓库. 2:假设有A,B两个人进行合作开发,此时A,B可以使用git clone 共享仓库路径进行克隆.此时A,B的室友仓库代码是一致的. ...

  2. 【SQL】sql update 多表关联更新方法总结

    #表结构: 1.表一:Test1 Id name age 1     2     2.表二:Test2 Id name age 1 小明 10 2 小红 8 #实现将表Test2的name和age字段 ...

  3. Ros使用Arduino 3用rosserial创建一个subscriber

    在前面的一节中,我们已经使用arduino创建了一个publisher节点,接下来将会用arduino来创建一个subscriber,接收电脑传去的信息并做出相应的反应. 1启动Arduino 将ar ...

  4. nodejs写入json文件,格式化输出json的方法

    假如我需要把data序列化成json字符串,然后写入data.json文件中,代码如下: let str = JSON.stringify(data) fs.writeFile('data.json' ...

  5. mysql 存儲emjoy表情是報錯Incorrect string value:

    解决方法: [mysqld] character-set-client-handshake=FALSE character-set-server=utf8mb4 collation-server=ut ...

  6. python开发技巧---列表、字典、集合值的过滤

    主要学习列表,字典,集合表达式的应用: 列表的解析式: 生成一个随机列表: In [4]: datalist = [randint(-10,10) for _ in range(10)] In [5] ...

  7. 一个网工的Linux学习过程

    机缘巧合下,在快要毕业时找到了一份网络工程师的工作,对于学习通信工程的我来说,也不算是跨专业就业吧.在入职之前也了解了一下网络工程师的学习路径,网络工程师是从事计算机信息系统的设计.建设.运行和维护工 ...

  8. 【SPOJ GSS】数据结构题选做

    SPOJ GSS1 题意:给一个序列以及一些询问,每个是问\([l,r]\)中最大连续子序列和是多少. 思路:这个问题是以下问题的基础. 我们考虑用线段树来解决这个问题. 首先我们来想想如果要求出最大 ...

  9. Dell Technology Summit(2018.10.17)

    时间:2018.10.17地点:北京国家会议中心

  10. [Python]Practice makes perfect

    Practice makes perfect 发音被儿子鄙视了.需要加强练习 以此自勉.