常用选项
    -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. 最新版本号MYSQL官网下载地址可是必需要注冊后才干下载

            因mysql5.0上运行函数不行,决定安装最新版本号的mysql,在网上找了些绿色版的.安装总报1067错误,网上的各种方法都试了,就是不行.浪费时间不说.郁闷死了,最后决定去官网,官 ...

  2. codeforces776D

    传送门 这题的意思就是原本有一个长度为n的01串,再给出m的长度为n的01串,要求你判定是否可以通过原串与m个串中的某些串xor使得原串到达一个状态.n,m小于1e5. 这题最初我发现不可做,因为这貌 ...

  3. LightOJ1220 —— 质因数分解

    题目链接:https://vjudge.net/problem/LightOJ-1220 1220 - Mysterious Bacteria    PDF (English) Statistics ...

  4. SVG-Android开源库——SVG生成Vector资源文件的编辑预览工具

    Vector矢量图在Android项目中的应用越来越广泛,但是如果你想用Android Studio自带的工具将SVG图片转化成Vector资源文件却是相当麻烦,首先能支持的SVG规范较少,其次操作流 ...

  5. 网络抓取功能实现 将获取的结果进行过滤并写入到TXT文档中

    下面是自己编写的 网络抓取功能实现 将获取的结果进行过滤并写入到TXT文档中 (以防忘记) 原创哟 import java.io.BufferedReader;import java.io.Buffe ...

  6. bleve搜索引擎是支持基于field搜索的

    Query String Query The query language query allows humans to describe complex queries using a simple ...

  7. Java丨时间判断谁前谁后

    直奔主题: String date_str1 = "2016-06-02 23:03:123"; String date_str2 = "2016-06-03 03:03 ...

  8. Building Objective-C static libraries with categories(ObjC、all_load、force_load)

    https://developer.apple.com/library/mac/qa/qa1490/_index.html    之所以使用该标志,和Objective-C的一个重要特性:类别(cat ...

  9. Json 不同语言的使用

    Javascript: 1.使用eval var parse_json_by_eval = function(str){ return eval('('+str+')'); } var value = ...

  10. Ubuntu下使用gcc编译c文件,未识别cos,sin

    Ubuntu下使用gcc编译c文件,虽然我调用了math.h的头文件,但是未识别cos,sin 报错:( fft.c ) /tmp/ccwXjD8C.o: In function `fft': fft ...