常用选项
    -E :开启扩展(Extend)的正则表达式。
  -i  :忽略大小写(ignore case)。
  -v :反过来(invert), 显示不包含匹配文本的所有行。
  -V 或 --version : 显示版本信息
        -n :显示行号
  -w :被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker
  -c  :显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到。
  -o  :只显示被模式匹配到的字符串。
        -r    :递归
        -f<规则文件> 或 --file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式
  --color :将匹配到的内容以颜色高亮显示。
  -A  n:显示匹配到的字符串所在的行及其后n行,after
  -B  n:显示匹配到的字符串所在的行及其前n行,before
  -C  n:显示匹配到的字符串所在的行及其前后各n行,context
[kumufengchun@localhost ~]$ cat sed.txt
my cat's name is betty This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
[kumufengchun@localhost ~]$ cat sed.txt | grep -i this
This is your This dog
my dog's name is This frank
This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
[kumufengchun@localhost ~]$ cat sed.txt | grep -v this
my cat's name is betty This is your This dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
[kumufengchun@localhost ~]$ cat sed.txt | grep -iv this
my cat's name is betty [kumufengchun@localhost ~]$ cat sed.txt | grep -n This
:This is your This dog
:my dog's name is This frank
:This is your fish
:my fish's name is This george
:This is your goat
:my goat's name is This adam
[kumufengchun@localhost ~]$ cat sed.txt | grep -w This
This is your This dog
my dog's name is This frank
This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
[kumufengchun@localhost ~]$ cat sed.txt | grep -c This [kumufengchun@localhost ~]$ cat sed.txt | grep -o This
This
This
This
This
This
This
This
[kumufengchun@localhost ~]$ cat sed.txt | grep -A frank
my dog's name is This frank This is your fish
[kumufengchun@localhost ~]$ cat sed.txt | grep -B frank This is your This dog
my dog's name is This frank
[kumufengchun@localhost ~]$ cat sed.txt | grep -C frank This is your This dog
my dog's name is This frank This is your fish 或的关系
[kumufengchun@localhost ~]$ cat sed.txt | grep -E "frank|adam"
my dog's name is This frank
my goat's name is This adam

通用类

[[:upper:]] [A-Z]
[[:lower:]] [a-z]
[[:digit:]] [0-9]
[[:alnum:]] [0-9a-zA-Z] 
[[:space:]] 空格或tab
[[:alpha:]] [a-zA-Z]
[[:punct:]]标点符号
替换每行的第二个This为小写this
[kumufengchun@localhost ~]$ sed -i 's/This/this/2g' sed.txt
[kumufengchun@localhost ~]$ grep '[[:lower:]]his' sed.txt
This is your this dog
[kumufengchun@localhost ~]$ grep '[[:upper:]]his' sed.txt
This is your this dog
my dog's name is This frank
This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam 在文件后边追加 $hello=
[kumufengchun@localhost ~]$ sed -i '$a\$hello=3' sed.txt
[kumufengchun@localhost ~]$ cat sed.txt
my cat's name is betty This is your this dog
my dog's name is This frank This is your fish
my fish's name is This george
This is your goat
my goat's name is This adam
$hello= [kumufengchun@localhost ~]$ grep "hello[^[:upper:]][[:digit:]]" sed.txt
$hello=
[kumufengchun@localhost ~]$ grep "hello[[:punct:]]" sed.txt
$hello=
[kumufengchun@localhost ~]$ grep "he[[:alnum:]]" sed.txt
$hello=
[kumufengchun@localhost ~]$ grep "hello[[:punct:]]" sed.txt
$hello=

shell学习(3)- grep的更多相关文章

  1. shell学习笔记

    shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...

  2. Shell学习之结合正则表达式与通配符的使用(五)

    Shell学习之结合正则表达式与通配符的使用 目录 通配符 正则表达式与通配符 通配符 通配符的使用 正则表达式 正则表达式 正则表达式的使用 通配符 正则表达式与通配符 正则表达式用来在文件中匹配符 ...

  3. Shell学习之Shell特性(一)

    Shell学习之Shell特性 目录 命令和文件自动补齐功能 命令历史记忆功能 history.上下键.!number.!string.!$.!! 别名功能 alias.unalias cp.~use ...

  4. shell学习笔记汇总

    1.shell脚本中函数使用 函数定义在前,调用在后,顺序反了就没有效果了.函数调用为:函数名 参数列表 函数内部通过以下变量访问函数的参数:shell脚本函数中: $0: 这个脚本的名字 $n: 这 ...

  5. shell 学习笔记2-shell-test

    一.字符串测试表达式 前面一篇介绍:什么是shell,shell变量请参考: shell 学习笔记1-什么是shell,shell变量 1.字符串测试表达式参数 字符串需要用""引 ...

  6. Shell学习笔记 - 正则表达式

    一.正则表达式是什么? 正则表达式是用于描述字符排列和匹配模式的一种语法规则.它主要用于字符串的模式分割.匹配.查找及替换操作. 二.正则表达式与通配符 1. 正则表达式 用来在文件中匹配符合条件的字 ...

  7. shell学习总结之自定义函数

    shell学习总结之自定义函数 Myfun (){ echo -n "now i is $i " ! [ "$i" ] && exit ; ec ...

  8. SHELL学习笔记----IF条件判断,判断条件

    SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外.  if list then           do something here   ...

  9. shell学习指南-阅读笔记

    shell学习指南真不是刚开始学习shell应该看得书,虽然其中讲了简单的linux命令,shell语法等,但是每章也有些深入和生僻地方,我想如果我刚学shell看到这样的地方一定会头疼的要死.或许也 ...

  10. 【转】shell学习笔记(一)——学习目的性、特殊字符、运算符等

    1 学习shell的目的性 写之前我们先来搞清楚为什么要学shell,学习要有目的性 shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题 shell可以实现自动化管理,让系统管理员的工作 ...

随机推荐

  1. HTML5 Canvas 画图入门

    HTML5 Canvas 画图入门 HTML5 Canvas 画图入门,仅供学习參考 <!DOCTYPE html> <html> <head> <meta ...

  2. (转)vim 访问系统剪贴板

    原文出处:http://vim.wikia.com/wiki/Accessing_the_system_clipboard Please review this tip: This tip was i ...

  3. Mysql数据库的打开和关闭

    Mysql数据库的打开和关闭: 选择计算机(win7)-右键管理 在新窗口选择--服务 5 找到mysql,然后右键-启动(停止)

  4. 怎样安装CentOS 6.6之三:磁盘分区的划分和修改

    安装 CentOS(或Linux)系统,最难的就是磁盘分区.磁盘分区需要根据自己的实际使用需要来规划,以达到最优的效果. 工具/原料   CentOS 6.6 安装包 VMware 虚拟机 一.划分方 ...

  5. ascall文件和二进制文件

    ascall文件可以打开让我们看你们的具体内容. 二进制文件打开我们看到的就是一堆乱码. ascall在换行时不同的平台不一样: windows上面用  \r\n linux上面用 \n 二进制的内容 ...

  6. Qt5.7不能加载MySql驱动问题.(需要重新编译驱动)

    转自:http://blog.csdn.net/qq_28851503/article/details/52422302 首先贴上我遇到的问题,如下: QSqlDatabase: QMYSQL dri ...

  7. bzoj3625

    fft 分治虽然是万能的,但是太慢了 分治是nlog^2n的,太慢了,于是我们用求逆和开根 设f(x)表示答案为x的方案数 c表示物品的生成函数 那么f=f*f*c+1 f*f表示左右儿子的方案数 c ...

  8. redis实例

    <?php Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 本篇文章,主要介绍利用PHP使用Redis ...

  9. JAVA 反射机制 获得 private 变量

    public class Triangle { // 定义三角形的三边 protected long lborderA = 0; protected long lborderB = 0; protec ...

  10. CF 345A Mike and Frog

    题目 自己歪歪的做法WA了好多发. 原题中每一秒都相当于 $x1 = f1(x1)$ $x2 = f2(x2)$ 然后这是一个定义域和值域都在[0,m-1]的函数,显而易见其会形成一个环. 而且环长不 ...