zoukankan      html  css  js  c++  java
  • Linux三剑客之grep命令

    一、命令详解

    grep命令时一个强大的文本搜索命令。

    1、命令格式

    • grep [参数] [关键字] <文件名>

    grep在过滤某个文件内容时后面需要跟文件名的,但是如果使用"|",后面不需要文件名。

    2、参数

    • -c 只输出匹配行的数量
    • -n 显示匹配行及行号
    • -v 显示不包含匹配行的文本内容

    更多内容通过man grep进行查看。

    二、实战

     1、文件内容

    [root@localhost thrid_dir]# cat grep_example.txt 
    aa AA
    aa BB
    cc CC
    dd DD

    2、参数实例

    [root@localhost thrid_dir]# grep -c aa grep_example.txt 
    2
    
    [root@localhost thrid_dir]# grep -n aa grep_example.txt 
    1:aa AA
    2:aa BB
    
    [root@localhost thrid_dir]# grep -v aa grep_example.txt 
    cc CC
    dd DD
    
    [root@localhost thrid_dir]# grep -vn aa grep_example.txt 
    3:cc CC
    4:dd DD

    三、扩展

    1、命令参数扩展

    • -r 递归查找
    • -i  忽略大小写
    # 递归查找文件内容
    [root@localhost thrid_dir]# grep -nr aa  /project/thrid_dir
    /project/thrid_dir/grep_example.txt:1:aa AA
    /project/thrid_dir/grep_example.txt:2:aa BB
    
    # 忽略大小写
    [root@localhost thrid_dir]# grep -nri aa  /project/thrid_dir
    /project/thrid_dir/grep_example.txt:1:aa AA
    /project/thrid_dir/grep_example.txt:2:aa BB

    2、egrep

    egrep是grep命令的扩展,它可以识别正则,基于正则进行内容匹配。

    # 过滤出文件中以a开头的行内容
    [root@localhost thrid_dir]# egrep '^a.*' grep_example.txt 
    aa AA
    aa BB
    作者:iveBoy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Linux命令之乐--nmap
    shell脚本学习总结09--分支与循环结构
    shell脚本学习总结08--比较与测试
    Linux命令之乐--test
    Linux命令之乐--read
    shell脚本学习总结06--数学计算
    Linux命令之乐--time
    Linux命令之乐--expr
    Linux命令之乐--seq
    Linux命令之乐--rename
  • 原文地址:https://www.cnblogs.com/shenjianping/p/14392136.html
Copyright © 2011-2022 走看看