Linux中Grep常用的15个例子

Grep命令主要用于从文件中查找指定的字符串。

首先建一个demo_file:

$ cat demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.

And this is the last line.

例01:从单个文件中查找指定的字符串

$ grep "this" demo_file

this line is the 1st lower case line in this file.

Two lines above this line is empty.

例02:从多个文件中查找指定的字符串

$ cp demo_file demo_file1

$ grep "this" demo_*

demo_file:this line is the 1st lower case line in this file.

demo_file:Two lines above this line is empty.

demo_file:And this is the last line.

demo_file1:this line is the 1st lower case line in this file.

demo_file1:Two lines above this line is empty.

demo_file1:And this is the last line.

例03:忽略大小写使用 grep -i

$ grep -i "the" demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

This Line Has All Its First Character Of The Word With Upper Case.

And this is the last line.

例04:在文件中匹配正则表达式

如果你能在实际使用正则表达式,能使效率大大提高。在下面的例子中,匹配了所有以lines开头,以empty结尾的行。

$ grep "lines.*empty" demo_file

Two lines above this line is empty.从Grep文档的来看,一个正则表达式必须遵循下面的匹配操作。

?         The preceding item is optional and matched at most once.

*          The preceding item will be matched zero or more times.

+         The preceding item will be matched one or more times.

{n}      The preceding item is matched exactly n times.

{n,}     The preceding item is matched n or more times.

{,m}    The preceding item is matched at most m times.

{n,m}  The preceding item is matched at least n times, but not more than m times.

例05:用grep -w来查找全匹配,不包括子字符串

比如说:用下面的例子搜索出来的例子包括"is","his"

$ grep -i "is" demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.

And this is the last line.

而用grep -iw搜索出来的结果如下: 注意,忽略大小。"IS","is"

$ grep -iw "is" demo_file

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

Two lines above this line is empty.

And this is the last line.例06:

例07:

例08:

例09:

例10:

例11:

例12:

例13:

例14:

例15:

原文链接: http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/

linux中Grep常用的15个例子,Linux中Grep惯用的15个例子相关推荐

  1. linux运维常用命令一句话(持续更新中)

    整理收集一些Linux运维管理.系统管理的常用命令,太多了记不住,只能记录下来方便日后查看.也可以和大家分享.如果你有好的一句话命令也贴出来吧.本文持续更新中. 1.linux启动过程 开启电源 -- ...

  2. 【Linux操作】常用命令整理(持续更新中...)

    目录 目录 一.解压缩 1..tar 2..tar.gz 3..tar.bz2 4.分卷解压缩 二.Linux下载torrent种子 三.查看并杀死进程 查看进程 杀死某进程 杀死全部python进程 ...

  3. linux 命令行 常用软件,12款最佳Linux命令行终端工具

    12款最佳Linux命令行终端工具 如果你跟我一样,整天要花大量的时间使用Linux命令行,而且正在寻找一些可替代系统自带的老旧且乏味的终端软件,那你真是找对了文章.我这里搜集了一些非常有趣的终端软件 ...

  4. Linux 后台开发常用命令目录(更新 ing)

    文章目录 前言 帮助命令 进程与作业管理 文本查看 文本编辑 编程相关 系统管理 文件传输 网络管理 文件与目录管理 磁盘管理 权限管理 用户与组管理 备份压缩 代码版本控制 性能检测 实用工具 前言 ...

  5. Linux目录及常用命令

    Linux目录及常用命令 文章目录 Linux目录及常用命令 前言 一.Linux目录 二.Linux基本命令 1.目录切换命令 2.目录操作命令 3.文件操作命令 4.压缩文件操作命令 5.权限操作 ...

  6. linux磁盘相关命令,Linux磁盘管理常用操作命令

    原标题:Linux磁盘管理常用操作命令 本文汇总了Linux磁盘管理基础知识.其中包括存储设备的挂载和卸载常用操作命令.自动挂载操作命令和磁盘分区操作命令等. 一.存储设备的挂载和卸载 存储设备的挂载 ...

  7. java request获取url_java中HttpServletRequest常用获取url、资源名等方法总结

    HttpServletRequest客户端获取请求,客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中.所以我们能够从对象中获取相应信息 端口 request.getSe ...

  8. 【Linux】vim常用命令

    [Linux]vim常用操作命令 文章目录 [Linux]vim常用操作命令 前言 一.一般命令模式 1.1 光标移动 1.2 查找与替换字符串 1.3 复制.删除.粘贴 1.4 其它小技巧 二.编辑 ...

  9. 【SCI\EI】英语论文中的常用字体是什么

    [SCI\EI]英语论文中的常用字体是什么 英语论文中所有英文的字体均采用"Times New Roman". 1.英文题目.摘要.关键词自成一页(1页即可),放在中文摘要页之后. ...

  10. linux开关机 按钮 间隔60秒,解决Linux性能问题的前60秒

    为了解决性能问题,你登入了一台Linux服务器,在最开始的一分钟内需要查看什么? 在Netflix我们有一个庞大的EC2 Linux集群,还有非常多的性能分析工具来监控和调查它的性能.其中包括用于云监 ...

最新文章

  1. java 连接mysql 8.0,java连接mysql 8.0的问题
  2. python程序设计狄光智_Python程序设计
  3. Github、Jekyll 搭建及优化静态博客方法指南
  4. 什么是mysql的主从复制?
  5. 【渝粤教育】国家开放大学2019年春季 1362应用语言学 参考试题
  6. 前端学习(1272):路由的基本概念和说明
  7. mac地址转换_mac电脑格式转化工具
  8. 公钥、私钥、数字签名、数字证书、对称与非对称算法、HTTPS
  9. .NET四种注释规范
  10. HDU1171-Big Event in HDU
  11. 微软开放Windows Phone 7中文开发中心
  12. mysql 查询优化 ~ 善用profie利器
  13. 根据姓名判断性别-人工智能
  14. matlab 200阶乘怎么表示,matlab阶乘怎么表示
  15. 成本360元的迷你物联网服务器有多香?
  16. 使用FreeHttp强制登出微信公众号登陆状态(实现~原理)
  17. 内网渗透小技巧(转载)
  18. 有序边表算法----计算机图形学
  19. 00.敏捷项目管理——前序笔记
  20. java语言保留结构和联合_Java 语言中取消了联合概念,保留了结构概念。( )_学小易找答案...

热门文章

  1. .NET Compact Framework 多线程下的等待事件
  2. GitChat · 前端 | Vue 组件库实践和设计
  3. UITextField对字符串的个数限制
  4. wcf双工通讯遇到的问题
  5. CMS设计和CMS选型(内容管理系统)
  6. 腾达A6无线路由器的几种连接模式
  7. python接口自动化测试一:http协议
  8. Go语言基础环境配置(windows)
  9. python计算蛋白质的质量
  10. 【测试】软件测试计划的编写