一、grep是什么?

  Linux grep命令是用于查找文件里符合条件行的shell命令。

二、为什么要使用grep?

  在查找文件内容时候,通过使用grep指定条件,可以快速定位到文件里字符串所在的行,提高效率。

三、grep的基本用法

grep命令有三个:grep,egrep,fgrep

grep:根据模式,搜索文本,并将复合模式的问本行显示出来。

-i: 忽视大小写
    --color: 高亮色显示匹配内容
    -v: 反向查找,没有被模式匹配到的行
    -o: 只显示被模式匹配到的字符串
    -A: 显示匹配到的行下面N行

-B: 显示匹配到的行上面N行
    -C: 显示匹配到的行上下行

正则表达式:

*: 匹配其前面的字符任意次

  1. [root@node1 ~]# cat test
  2. ab
  3. aab
  4. acb
  5. adb
  6. anmb
  7. nmbnmbnmnbbbsdfb
  8. [root@node1 ~]# egrep 'a*b' test   # 其面前字符的任意长度例如:b ab aab aaaab
  9. ab
  10. aab
  11. acb
  12. adb
  13. anmb
  14. nmbnmbnmnbbbsdfb

?: 匹配其前面的字符1次或0次

  1. [root@node1 ~]# egrep 'a?b' test   # 匹配其前面的字符1次或者0次,如:ab b
  2. ab
  3. aab
  4. acb
  5. adb
  6. anmb
  7. nmbnmbnmnbbbsdfb

.: 匹配任意单个字符

  1. [root@node1 ~]# egrep 'r..t' /etc/passwd   # 匹配如:root rabt r/ft
  2. root:x:::root:/root:/bin/bash
  3. operator:x:::operator:/root:/sbin/nologin
  4. ftp:x:::FTP User:/var/ftp:/sbin/nologin

[-]: 匹配指定范围内的任意单个字符

  1. [root@node1 ~]# netstat -ntplu | egrep '4[0-9]00'
  2. tcp 10.0.0.10: 0.0.0.0:* LISTEN /httpd
  3. tcp 10.0.0.10: 0.0.0.0:* LISTEN /httpd
  4. tcp 10.0.0.10: 0.0.0.0:* LISTEN /httpd
  5. tcp 10.0.0.10: 0.0.0.0:* LISTEN /httpd

[^-]: 匹配指定范围外的任意单个字符

  1. [root@node1 ~]# netstat -ntplu | egrep '4[^1-3]00'
  2. tcp 10.0.0.10: 0.0.0.0:* LISTEN /httpd

{}: 匹配其前面字符的多少次

  1. [root@node1 ~]# egrep 'a{1,2}b' test # 匹配内容 ab, aab
  2. ab
  3. aab

位置锚定:

^: 锚定行首,此字符后面的任意内容必须出现在行首

  1. [root@node1 ~]# egrep '^root' /etc/passwd
  2. root:x:::root:/root:/bin/bash

$: 锚定行尾,此字符后面的任意内容必须出现在行尾

  1. [root@node1 ~]# egrep 'shutdown$' /etc/passwd   # 匹配以shutdown结尾的行
  2. shutdown:x:::shutdown:/sbin:/sbin/shutdown

^$: 空白行

  1. [root@node1 ~]# cat abc.txt
  2. a
  3.  
  4. b
  5.  
  6. c
  7. [root@node1 ~]# egrep '^$' abc.txt
  8.  
  9. [root@node1 ~]# egrep '^$' abc.txt | wc -l   # 可以统计出来有两行是空格行

\<或\b: 其后面的任意字符必须作为单词首部出现

  1. [root@node1 ~]# cat test.txt
  2. This is root.
  3. The user is mroot.
  4. rooter is a dog's name.
  5. mrooter is not a word.
  6. [root@node1 ~]# egrep 'root\>' test.txt   # 匹配词尾的
  7. This is root.
  8. The user is mroot.

\>或\b: 其前面的任意字符必须作为单词尾部出现

  1. [root@node1 ~]# cat test.txt   # 记住这里的词首不是行首。因此 This is root. 是没问题的
  2. This is root.
  3. The user is mroot.
  4. rooter is a dog's name.
  5. mrooter is not a word.
  6. [root@node1 ~]# egrep '\<root' test.txt
  7. This is root.
  8. rooter is a dog's name.

[ Linux 命令 ] grep的更多相关文章

  1. linux 命令grep

    linux 命令grep grep命令用来搜索文本,或从给定的文件中搜索行内包含了给定字符串或单词的文件.通常来说,grep显示匹配的行.使用grep来搜索包括一个或多个正则表达式匹配到的文本行,然后 ...

  2. 【linux】linux命令grep + awk 详解

    linux命令grep  +  awk 详解 grep:https://www.cnblogs.com/flyor/p/6411140.html awk:https://www.cnblogs.com ...

  3. Linux 命令 - grep: 正则搜索文本

    grep 搜索文本文件中与指定正则表达式匹配的行 命令格式 grep [OPTIONS] PATTERN [FILE...] 命令参数 Generic Program Information --he ...

  4. Linux 命令——grep | 正则表达式

    感觉讲的很详细,瞬间懂了grep,正则. from: here 简介 grep (global search regular expression(RE) and print out the line ...

  5. 菜鸟学Linux命令:grep配合ls等使用

    linux grep命令 (global search regular expression(RE) and print out the line )是一种强大的文本搜索工具,它能使用正则表达式搜索文 ...

  6. Linux命令-grep

    grep命令用于对文本进行搜索,格式为“grep [选项] [文件]” 搜索某个关键词:"grep 关键词 文本文件" 参数说明 -b 将可执行文件当做文本文件来搜索 -c 仅显示 ...

  7. (转)Linux命令grep

    场景:grep命令在文件搜索中经常会使用到,所以熟练掌握该命令对于日常日志搜索相当有必要! Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.g ...

  8. 日志快速筛选 之 linux命令grep|uniq|wc|awk

    以前我个人的观念是,在线上运行的东西尽量不要记什么流水日志. 但是后来我变了,发现在线上记日志是一个绝对有必要的东西,尤其是在当下很流行的微服务的推动下,没有日志的帮助,犹如一个睁眼瞎,排查问题基本靠 ...

  9. linux命令-grep+正则表达式用法

    目标文件/etc/passwd,使用grep命令或egrep 1.显示出所有含有root的行:egrep 'root' passwd 2.输出任何包含bash的所有行,还要输出紧接着这行的上下各两行的 ...

随机推荐

  1. const 常量与 define常量的区别

    c++中的常量可以使用const定义,也可以使用#define宏定义的方式:二者区别如下: - **区别** 1. const定义的常量有自己的数据类型,编译器可以对其进行严格的类型检查:但是defi ...

  2. sqlserver 找出字符第N次出现的位置

    [1编写函数]CREATE FUNCTION IndexOf(@str VARCHAR(500),@value VARCHAR(50),@posIndex INT)RETURNS int AS BEG ...

  3. UGUI 代码 动态添加 Event Trigger 的事件

    Additionally, if you need more than just the events provided by default, I'd suggest instead attachi ...

  4. scp源码浅析

    背景: 经常使用scp传文件,发现它真的很给力,好奇心由来已久! 恰好接到一个移植SSH服务到专有网络(非IP网络)的小任务,完成工作又能满足好奇心,何乐而不为! 我只从源码浅浅的分析一下,后续有更多 ...

  5. Ext.net中TreePanel动态生成

    这个问题可以参考官网例子:http://examples2.ext.net/#/TreePanel/Basic/Built_in_CodeBehind/ 贴一段本人程序中用到的动态生成核心代码: Ex ...

  6. perf原理再看

    vim ./arch/x86/kernel/hw_breakpoint.c perf如何控制采样的频率 perf采样不同的事件,得到的不是一样 cycles: 向PMU中增加不同的函数,增加不同 使用 ...

  7. 用Web Service实现客户端图片上传到网站

    由于项目需要,通过本地客户端,把图片上传到网站.通过webservice. 这是客户端代码: private void btnimg_Click(object sender, EventArgs e) ...

  8. [bzoj5321] [Jxoi2017]加法

    Description 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个 ...

  9. 机器学习模型-支持向量机(SVM)

    二.代码实现 import numpy as np from sklearn import datasets from sklearn.model_selection import train_tes ...

  10. [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机

    Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...