Linux命令之grep

Linux 查询命令四剑客:awk grep find sed

grep /grep/

n. UNIX工具程序;可做文件内的字符串查找

Linux grep命令用于查找文件里符合条件的字符串 。

grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

grep命令的常用格式为:grep [选项] ”模式“ [文件]

grep家族总共有三个:grep,egrep,fgrep。

在这里插入图片描述

  • -A n:显示匹配到的字符串所在的行及其后n行,after

    # 注:要匹配的是单个字符串,引号加不加都可,多个字符串需要加引号
    # *:代表当前文件夹下的所有文件
    
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -A 2 one 1.txt
    one dream
    
    hello world!
    
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -A 3 one 1.txt
    one dream
    
    hello world!
    hello world
    
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -A 1 python *
    1.py:python
    1.py-111gujg
    --
    2.py:python
    2.py-hello
    --
    3.py:python
    
  • -B n:显示匹配到的字符串所在的行及其前n行,before

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -B 1 one 1.txt
    One world,
    one dream
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -B 2 one 1.txt
    world
    One world,
    one dream
    
  • -C n:显示匹配到的字符串所在的行及其前后各n行,context

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -C 2 dream 1.txt
    world
    One world,
    one dream
    
    hello world!
    
  • -c 计算找到“搜索字符串”的行数

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -c hello 1.txt
    3
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -c hello *
    1.py:1
    1.txt:3
    2.py:1
    3.py:1
    
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -c 'hello' 1.txt
    3
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -c "hello" 1.txt
    3
    
  • -d 目录的处理方式(读取、递归、跳过) read recurse skip

    • recurse: 递归查找目录下的文件
    [root@iz2ze6yoko50h2dkqgxk56z ~]# grep -d recurse world /home
    /home/1.py:world!
    /home/python/1.py:world
    /home/1.txt:world
    /home/1.txt:One world,
    /home/1.txt:hello world!
    /home/1.txt:hello world
    /home/2.py:world
    /home/3.py:world
    
  • -o 指数出匹配的内容

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -o hello 1.txt
    hello
    hello
    hello
    hello
    
  • -i -y 不区分大小写

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -i 'One' 1.txt
    One world,
    one dream
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -y 'One' 1.txt
    One world,
    one dream
    
  • -n 显示匹配内容及行号

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -n hello 1.txt
    1:hello hello
    6:hello world!
    7:hello world
    
  • -r 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将回报信息并停止动作

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -r world *
    1.py:world!
    1.txt:world
    1.txt:One world,
    1.txt:hello world!
    1.txt:hello world
    2.py:world
    3.py:world
    python/1.py:world
        
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -r world /home/python
    /home/python/1.py:world
    
  • -H 打印每个匹配项的文件名。当需要搜索多个文件时,这是默认设置。

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -H hello *
    1.py:hello
    1.txt:hello hello
    1.txt:hello world!
    1.txt:hello world
    2.py:hello
    3.py:hello
    
  • -h 在输出中取消文件名的前缀。这是默认情况下,只有一个文件(或只有标准输入)搜索

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -h world *
    world!
    world
    One world,
    hello world!
    hello world
    world
    world
    
  • -v 反向选择,即没有‘搜索字符串’内容的行

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -v world 1.py
    en
    hello
    python
    
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -n -v world 1.py
    1:en
    2:hello
    4:python
    5:111gujg
    
  • -V 显示软件版本信息

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -V
    grep (GNU grep) 2.20
    Copyright (C) 2014 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
    
  • -l 列出文件内容符合指定的范本样式的文件名称

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -l en 1.py 2.py
    1.py
    
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -l python *
    1.py
    2.py
    3.py
    
  • -L 列出文件内容不符合指定的范本样式的文件名称

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -l en 1.py 2.py
    1.py
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -L en 1.py 2.py
    2.py
    
    
  • -m num 指定最大数目的匹配行

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep hello 1.txt
    hello hello
    hello world!
    hello world
    
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -m2 hello 1.txt
    hello hello
    hello world!
    
  • -w 精确搜索,可以说准确性搜索, grep -w world *,只能匹配到出现 world单词 的行。

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -w world *
    1.py:world!
    1.txt:world
    1.txt:One world,
    1.txt:hello world!
    1.txt:hello world
    2.py:world
    3.py:world
    
  • -x 完全匹配输出,只会输出某一行存在hello字符串,并且此行仅包含hello的内容。假设a.txt中有一行“hello all”,执行上述命令,此行不会被搜索到。

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -x hello *
    1.py:hello
    2.py:hello
    3.py:hello
    grep: hello: Is a directory
    grep: python: Is a directory
    grep: world: Is a directory
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep hello *
    1.py:hello
    1.txt:hello hello
    1.txt:hello world!
    1.txt:hello world
    2.py:hello
    3.py:hello
    
  • -E 扩展 grep,即 egrep,可以使用扩展正则表达式

    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -E '^[e]' *
    1.py:en
    grep: hello: Is a directory
    grep: python: Is a directory
    grep: world: Is a directory
            
    [root@iz2ze6yoko50h2dkqgxk56z home]# egrep '^[e]' *
    1.py:en
    grep: hello: Is a directory
    grep: python: Is a directory
    grep: world: Is a directory
    
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep -E '^[e]' *
    1.py:en
    grep: hello: Is a directory
    grep: python: Is a directory
    grep: world: Is a directory
            
    [root@iz2ze6yoko50h2dkqgxk56z home]# egrep '^[e]' *
    1.py:en
    grep: hello: Is a directory
    grep: python: Is a directory
    grep: world: Is a directory
    
  • –color=auto 搜索关键词显示颜色 ,默认是红色

  • 正则表达式详解:

    * 前一个字符匹配0次或多次;
    
    . 匹配除了换行符以外任意一个字符;
    
    .* 代表任意字符;
    
    ^ 匹配行首,即以某个字符开头;
    
    $ 匹配行尾,即以某个字符结尾;
    
    [] 匹配中括号里的任意指定字符,但只匹配一个字符;
    
    [^] 匹配除中括号以外的任意一个字符;
    
    [] 匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。    
    
    [^] 匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。    
    
    ..  标记匹配字符,如'love',love被标记为1。    
    
    \<      #锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。    
    
    \>      #锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。    
    
    x\{m\}  #重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。    
    
    x\{m,\}  #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。    
    
    x\{m,n\}  #重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。
    [root@iz2ze6yoko50h2dkqgxk56z home]# grep '[1]\{1,4\}' *
    1.py:111gujg
    
    \w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。   
    
    \W    #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。   
    
    \b    #单词锁定符,如: '\bgrep\b'只匹配grep。
    
  • 常用GREP工具企业演练案列:

    grep 'test[53]' 1.txt 以字符 test 开头,接5或者3的行;
    
    grep '^[^e]' 1.txt 显示输出不是以 e 开头的行;
    
    grep '[Mm]ay' 1.txt 匹配 May 或 may 开头的行;
    
    grep 'K…D' 1.txt 匹配K,三个任意字符,紧接D的行;
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值