1. grep语法及其参数说明

  grep是文本搜索工具,能根据用户指定的'PATTERN模式'目标文本进行逐行匹配检查,注意grep默认会以 行 为单位打印匹配到的行.

  以下是grep命令的语法及常用参数:

grep [OPTIONS] PATTERN [FILE...] 
   选项    模式   文件
###参数及含义
--color=auto  #<==匹配成功显示颜色
-v #<==排除
-i #<==忽略大小写
-n  #<==显示匹配行及其行号
-o #<==仅显示匹配到的字符串
-w  #<==只匹配过滤的单词
-q #<==静默参数,不输入任何信息
-r             #<==递归过滤目录下的文件
-A #<==after,后n行
-B #<==before,前n行
-C #<==前后各n行
-E #<== 相当于egrep,同时过滤多个,中间使用 | 分隔

2. grep命令常用用法

######命令参数常用用法:  
[root@test ~]# cat a.txt
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@test ~]# grep -n 'ntp' a.txt  #<==显示字符串所在行及其行号
3:ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@test ~]# grep -v '^ntp' a.txt  #<==排除用法,将以ntp开头的行排除后打印输出
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@test ~]# grep -i 'Privilege' a.txt    #<==忽略大小写,输出字符串匹配到的行
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@test ~]# grep -o 'Privilege' a.txt    #<==仅仅显示匹配到的字符串
Privilege
[root@test ~]# grep -w 'Privilege' a.txt    #<==很少用,匹配单词,但有时用于脚本中匹配指定的用户
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@test ~]# seq 10 >a.txt
[root@test ~]# grep -E "1|2" a.txt  #同时过滤多个字符串所在的行
[root@test ~]# grep -A 3 '5' a.txt  #<==显示字符串5及后面3行
5
6
7
8
[root@test ~]# grep -B 3 '5' a.txt  #<==显示字符串'5'及之前3行
2
3
4
5
[root@test ~]# grep -C 3 '5' a.txt  #<==显示字符串'5'及前后3行
2
3
4
5
6
7
8
######配合正则表达式常用用法(注意:使用egrep就无需 \ 反斜线转义,grep使用正则需要 \ 转义)
[root@test ~]# cat a.txt
abxy
xay
xxxxxxxxy
x
[root@test ~]# grep 'x*?y' a.txt  #<==注意,这里不会匹配到任何内容,此时?代表单个字符
[root@test ~]# grep 'x*y' a.txt  #<==贪婪模式(最长匹配原则)匹配
abxy
xay
xxxxxxxxy
[root@test ~]# grep 'x*\?y' a.txt  #<==此时问号是正则,代表匹配前面字符0次或1次
abxy
xay
xxxxxxxxy
[root@test ~]# grep 'a.*y' a.txt   #<==.*代表任意内容
abxy
xay
[root@test ~]# grep 'x\?y' a.txt
abxy
xay
xxxxxxxxy
[root@test ~]# grep 'x\+y' a.txt  #<==+号匹配前面字符x至少1次
abxy
xxxxxxxxy
#####匹配次数
[root@test ~]# tail -5 /etc/passwd >a.txt
[root@test ~]# cat a.txt
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@test ~]# grep -o '[a-z]\{3\}t' a.txt  #<==匹配文本中,t字符串的前面有3个随意小写字母的字符串
ount
abrt
abrt
arat
empt
post
post
[root@test ~]# grep -o '[a-z]\{3,5\}t' a.txt  #<==匹配,t字符串的前面有3-5个小写字母的字符串
[root@test ~]# grep -o '[a-z]\{,5\}t' a.txt  #<==匹配,t字符串的前面有至多5个小写字母的字符串
[root@test ~]# grep -o '[a-z]\{3,\}t' a.txt  #<==匹配,t字符串的前面有至少3个小写字母的字符串
#####位置锚定
[root@test ~]# cat a.txt
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@test ~]# grep -o '\<[a-z]\{3\}t' a.txt   #<==必须出现单词首部
abrt
abrt
empt
post
post
[root@test ~]# grep -o '\<[a-z]\{3\}t\>' a.txt  #<==锚定  单词尾部
abrt
abrt
[root@test ~]# ifconfig eth0|sed -n '2p'|grep -o '\<10.0.0.30\>'  #<==grep功能不擅长过滤此种内容
10.0.0.30
######生产常用于去除空行和以#号开头的行
[root@test ~]# grep -Ev "^$|^#" /etc/yum.repos.d/CentOS-Base.repo

  

grep知识及常用用法梳理的更多相关文章

  1. sed知识及常用用法梳理

    1.sed命令简介及其参数说明 sed流编辑器,擅长对文本进行增删改查,过滤指定的字符串和取指定行,也可以在行中字符串前后插入内容,功能非常强大. 注意:sed默认只支持基本的正则表达式,如果要想支持 ...

  2. grep参数说明及常用用法

    grep参数说明及常用用法 趁着午休的时间把自己经常使用的一些grep命令整理一下. 方便以后查看. 后续会逐步把awk/sed/find等常用的命令理一理. 增强下记忆. 也算是对得起自己了. ^^ ...

  3. grep参数说明及常用用法(转)

    转:https://www.cnblogs.com/leo-li-3046/p/5690613.html grep常用参数说明 grep [OPTIONS] PATTERN [FILE...] gre ...

  4. grep 的一些常用用法

    打印匹配到的上下5行 grep -C 5 'root' /etc/passwd            上下5行 grep -A 5 'root' /etc/passwd            afte ...

  5. grep常用用法

    grep常用用法 [root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename 选项与参数: -a :将 binary 文件以 text 文件 ...

  6. 预备知识-python核心用法常用数据分析库(上)

    1.预备知识-python核心用法常用数据分析库(上) 目录 1.预备知识-python核心用法常用数据分析库(上) 概述 实验环境 任务一:环境安装与配置 [实验目标] [实验步骤] 任务二:Pan ...

  7. [转]ssh常用用法小结

    ssh常用用法小结 1.连接到远程主机: 命令格式 : ssh name@remoteserver 或者 ssh remoteserver -l name 说明:以上两种方式都可以远程登录到远程主机, ...

  8. ssh常用用法小结

    ssh常用用法小结 1.连接到远程主机: 命令格式 : ssh name@remoteserver 或者 ssh remoteserver -l name 说明:以上两种方式都可以远程登录到远程主机, ...

  9. iptables-25个常用用法【转】

    本文介绍25个常用的iptables用法.如果你对iptables还不甚了解,可以参考上一篇iptables详细教程:基础.架构.清空规则.追加规则.应用实例,看完这篇文章,你就能明白iptables ...

随机推荐

  1. Activiti工作流引擎开发系列

    Activiti工作流引擎开发系列-01 作者:Jesai 没有伞的孩子,只能光脚奔跑! 前言: 初次接触工作流这个概念是自从2014年11月份开始,当时是由于我的毕业设计需要,还记得当时我毕业设计的 ...

  2. request session

    例子 url = 'http://beanhome.com/user/login' header = { "Content-Type": 'application/json', & ...

  3. Qt Installer Framework翻译(3-4)

    更新组件 下图说明了用于更新已安装组件的默认工作流程: 本节使用在macOS上运行的Qt 5维护工具为例,来演示用户如何更新已安装组件. 启动更新程序 用户启动维护工具时,将打开"简介&qu ...

  4. Redis(五):hash/hset/hget 命令源码解析

    Redis作为nosql数据库,kv string型数据的支持是最基础的,但是如果仅有kv的操作,也不至于有redis的成功.(memcache就是个例子) Redis除了string, 还有hash ...

  5. C0nw4y's L!f3 G4me 代码实现

    这是我转载的博客,关于这个游戏的介绍.估计没人能get到这个游戏的blingbling的地方吧.还是蛮惊叹的. 因为这里网络实在惨淡,闲来无事实现了下这个游戏,UI尽量美化了,可惜python配置不知 ...

  6. 「 从0到1学习微服务SpringCloud 」08 构建消息驱动微服务的框架 Spring Cloud Stream

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  7. 【javaWeb】sendRedirect和forward原理及区别总结

    一.原理.  1. Forward        该图的交互过程如下: ① 浏览器访问Servlet1. ② Servlet1想让Servlet2对客户端的请求进行响应,于是调用forward()方法 ...

  8. 电脑开机后多了OneKey Ghost启动选项怎么解决

    原文地址:http://www.xitongcheng.com/jiaocheng/dnrj_article_18745.html 大多数用户在使用OneKey Ghost安装电脑系统后,会在开机启动 ...

  9. Kdenlive-简单的操作

    版权声明:原创文章,未经博主允许不得转载 前章:https://www.cnblogs.com/weilinfox/p/12246123.html 尽管是简单操作,但内容比较多.可以一边自己尝试编辑一 ...

  10. mysql5.7的基本使用

    mysql的基本使用:最简单的增删改查 (建议用类似记事本的东西写代码,错了容易改) 以下就是这篇文章的代码 一,增和查   CREATE DATABASE one; 新建了一个名为one的数据库 S ...