今天在纠结grep用法时候,由于讲解的教材比较少,纠结了较长的时间。最终还是攻下了,所以拿出来给大家分享。

grep

显示匹配一个或多个模式的文本行,时常会作为管道后的第一步,以便对匹配上的数据做进一步处理。

最常见用法,查询文件内字符串

[root@localhost /]# grep root /etc/shadow

root:$1$HFDnk5hm$DSAc4IUls1yUyocXFNQ.A.:15141:0:99999:7:::

[root@localhost /]#

参数

-E  使用扩展正则表达式进行匹配,使用grep –E 代替传统的扩展正则表达式 egrep

扩展正则表达式和正则表达式的区别:  (-E选项的作用) +  匹配一个或多个先前的字符

[root@localhost space]# touch test

[root@localhost space]# vim test

aaaredhat

bbbredhat

yyyredhat

zzzredhat

redhataaa

redhatbbb

redhatyyy

redhatzzz

:wq

[root@localhost space]# cat test|grep –E '[a-h]+redhat'

aaaredhat

bbbredhat

[root@localhost space]#

[root@localhost space]# cat test|grep -E 'redhat+[h-z]'

redhatyyy

redhatzzz

[root@localhost space]#

当去掉-E选项的时候,正则表达式是不支持这样查询的。

[root@localhost space]# cat test|grep 'redhat+[a-z]'

[root@localhost space]# ?   匹配零个或多个先前的字符

[root@localhost space]# cat test|grep -E 'r?aaa'

aaaredhat

redhataaa

[root@localhost space]# cat test|grep 'r?aaa'

[root@localhost space]# a|b|c  匹配a或b或c

[root@localhost space]# cat test|grep -E 'b|z'

bbbredhat

zzzredhat

redhatbbb

redhatzzz

[root@localhost space]# cat test|grep 'b|z'

[root@localhost space]# () 分组符号

[root@localhost space]# cat test|grep -E 'redha(tz|ta)'

redhataaa

redhatzzz

[root@localhost space]# cat test|grep 'redha(tz|ta)'

[root@localhost space]# x{m} 重复字符x,至少m次   (如果用正则表达式,格式为x\{m,\})

[root@localhost space]# cat test|grep -E 'a{3}'

aaaredhat

redhataaa

[root@localhost space]# cat test|grep 'a\{3,\}'

aaaredhat

redhataaa

[root@localhost space]#

-F  使用固定字符串进行匹配 grep –F 取代传统的 fgrep 。 使用正则表达式; 默认情况下 grep 使用的就是正则表达式,grep = grep –F

-e  通常,第一个非选项的参数会指定要匹配的模式,这个模式以-号开头时,grep就会混淆,而-e选项就将它确定为参数

[root@localhost space]# cat test|grep -e '-test'

-test

[root@localhost space]# cat test|grep '-test'

grep:无效选项 -- t

Usage: grep [OPTION]... PATTERN [FILE]...

Try `grep --help' for more information.

[root@localhost space]#

-i  模式匹配时,忽略大小写

[root@localhost space]# cat test|grep -i test

-test

-TEST

[root@localhost space]#

-l  列出匹配模式的文件名称  (列出当前目录下含有test字符串的文件)

[root@localhost space]# grep -l 'test' ./*

./test

./test2

[root@localhost space]#

-q  静默模式,如果匹配成功,则grep会离开,不显示匹配。

[root@localhost space]# grep test ./test

-test

[root@localhost space]# grep -q test ./test

[root@localhost space]#

-s  不显示错误信息

-v  显示不匹配的项

[root@localhost space]# cat test|grep test

-test

[root@localhost space]# cat test|grep -v test

-TEST

[root@localhost space]#

linux脚本grep,linux shell 脚本之深入浅出的grep的用法相关推荐

  1. linux shell脚本 定义变量,Shell脚本应用 - 编制shell脚本、shell变量

    随着Linux系统在企业中的应用越来越多,服务器的自动化管理也变得越来越重要.在Linux服务器的自动化维护工作中,除了计划任务的设置以外,Shell脚本的应用也是非常重要的一部分.作为Linux系统 ...

  2. 《Linux命令行与shell脚本大全》笔记

    初识Linux Shell 什么是Linux Linux可划分为以下四部分: Linux内核 GNU工具 图形化桌面环境 应用软件 深入探究Linux内核 内核主要负责以下四种功能: 系统内存管理 软 ...

  3. 【Linux命令行与Shell脚本编程】第五章 理解 Shell 父子关系 后台进程 协程

    Linux命令行与Shell脚本编程 第五章 理解 Shell 文章目录 Linux命令行与Shell脚本编程 五,理解 Shell 5.1,shell的类型 5.2,shell的父子关系 5.2.1 ...

  4. linux运维自动化脚本,linux运维自动化shell脚本小工具

    linux运维shell 脚本小工具,如要分享此文章,请注明文章出处,以下脚本仅供参考,若放置在服务器上出错,后果请自负 1.检测cpu剩余百分比 #!/bin/bash #Inspect CPU # ...

  5. 《Linux命令行与shell脚本编程大全》笔记一

    第一章 初始 Linux shell Linux内核主要四种功能: 1.系统内存管理 2.软件程序管理 3.硬件设备管理 4.文件系统管理 shell:交互工具. 第三章 基本的bash shell ...

  6. 【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-汇总 ( Chapter17-ChapterB )

    十七.创建函数 bash shell 提供了用户自定义函数功能,可以将 shell 脚本代码放入函数中封装起来. 函数是一个脚本代码块,你可以为其命名并在脚本中的任何位置重用它.每当需要在脚本中使用该 ...

  7. linux csh 安装,linux安装gcc的shell脚本

    原标题:linux安装gcc的shell脚本 GCC是GUN Compiler Collection的简称,除了编译程序之外,它还含其他相关工具,它能把易于人类使用的高级语言编写的源代码构建成计算机能 ...

  8. linux 命令行与shell脚本编程大全

    linux 命令行与shell脚本编程大全 第一章 Linux LiveCD就是从cd读取的系统,由于没法将数据写入到cd,所以一旦重启,之前操作过后的一切数据都会丢失. 第二章 第三章 1.man手 ...

  9. 【2018深信服 醒狮计划】《Linux命令行与Shell脚本编程大全》学习笔记

    2018深信服"醒狮计划"笔记 第3周(5.02-5.13) 课程 必修 选修 基本要求 Shell编程 <Linux命令行与Shell脚本编程大全> <Perl ...

  10. Linux命令行与shell脚本编程大全:第2版

    <Linux命令行与shell脚本编程大全:第2版> 基本信息 作者: (美)布卢姆(Blum,R.) 布雷斯纳汉(Bresnahan.C.) [作译者介绍] 译者: 武海峰 丛书名: 图 ...

最新文章

  1. python使用pyodbc连接sql server 2008
  2. linux下的QQ执行玩法:pidgin-lwqq
  3. 转:ORACLE的JDBC连接方式:OCI和THIN
  4. SAP Spartacus SeoMetaService 的单元测试代码
  5. Git笔记(13) 分支管理
  6. 9_python基础—数据序列(公共方法)
  7. zookeeper集群节点热扩容和迁移详解
  8. div css将文字居中显示图片,css文字居中、图片居中、div居中解决方案
  9. java求1~20阶乘之和
  10. Redisson读写锁
  11. RedisConnectionFactory is required异常解决
  12. 威纶通触摸屏232脚位_威纶通触摸屏使用手册
  13. 使用cobra创建cli命令行工具
  14. MySQL中建表时 pk、nn、qu、b、un、zf、ai、g代表的意思
  15. 2020身高体重标准表儿童_2020儿童身高体重标准表
  16. Qt中多线程的一种使用
  17. 【干货】玩运营不可不知的经典“事件营销”案例
  18. Windows系统制作安装
  19. iOS 检测设备所连接的WiFi、4G等网络是否真的可以连通
  20. teamspeak3服务器搭建_TeamSpeak安装设置及服务器配置

热门文章

  1. 小敏同学利用计算机设计,福建省晋江一中、华侨中学2015-2016学年七年级数学上学期期中质量检测试题(无答案) 华东师大版...
  2. php5.5 安装pdo oci,PHP5.3连接Oracle客户端及PDO_OCI模块的安装方法
  3. python windows自动化 爬虫_Python体系(网络爬虫、人工智能、WEB开发、自动化运维)详细介绍...
  4. HDU 1317 XYZZY
  5. booleanfield()和booleanfield(default=True)在数据库的表中无法插入
  6. Fiddler-2 Fiddler抓包原理
  7. java初学者笔记总结day7
  8. ubuntu - 14.04,配置GOPATH(GO语言开发代码存放目录)
  9. 6410裸机开发教程下载
  10. [教程]控制反转(IoC)与依赖注入(DI)