zoukankan      html  css  js  c++  java
  • grep Demo

      最近在学习Linux的几个非常强大的命令awk, sed, grep. 之前对这些命令只是有非常皮毛的了解.

    最近稍微深入的对这些命令进行一些学习.grep的主要功能如下:

      @1: 正则匹配文件中的某个(某些)字符串.

      @2: grep具备递归搜索文件/目录(文件的内容)功能.

    #!/bin/bash
    #File: grepDemo.sh
    #Author: lxw
    #Time: 2014-08-21
    #Usage: Demonstration for grep.
    
    main(){
        #The following 2 lines are identical.
        grep "seven" test.txt|wc -l
        grep "seven" test.txt -c    #-c: count the number of lines(not words) that contain "seven"
    
        #Match everything.
        grep ".*" test.txt --color
        #-w: word.Match the word.
        grep -w "seven" test.txt --color
    
        #<seven: words begin with "seven"
        grep "<seven" test.txt --color
        grep "seven>" test.txt --color
    
        #^seven: lines(not words) begin with "seven"
        grep "^seven" test.txt --color
        grep "seven$" test.txt --color
    
        #-C num: print num lines around the matched line.
        grep -C 1 "twentyseven" test.txt --color
        #-A num: print num lines after the matched line.
        grep -A 1 "twentyseven" test.txt --color
        #-B num: print num lines before the matched line.
        grep -B 1 "twentyseven" test.txt --color
    
        #-E: extended-regexp.    -E is essential here. The 2nd method is better.
        grep -E "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" /etc/resolv.conf  --color
        grep -E "[0-9]{1,3}(.[0-9]{1,3}){3}" /etc/resolv.conf  --color
    
        #-v: invert-match. Print the non-matching lines.
        grep -E "[0-9]{1,3}(.[0-9]{1,3}){3}" /etc/resolv.conf | grep -v "#" --color
        #-o: only-matching. Print only the matched parts, with each part on a single line.
        grep -oE "[0-9]{1,3}(.[0-9]{1,3}){3}" /etc/resolv.conf --color
    
        #|: or.
        vmstat|grep -E "procs|swpd" --color
    
        #[:upper:] upper alphabets. [:digit:] numbers 0-9.
        grep "[[:upper:]]" test.txt    --color
        grep "[[:digit:]]" test.txt --color
    
        #Find the lines that contains six/seven/eight more than once.
        #NOTE:parenthesis is essential here. Otherwise, what do you think of "1"?
        grep -E "(six|seven|eight).*1" test.txt --color
    
        #-r:recursive    -i:ignore-case    -n:line number
        grep -rin "lxw" /home/lxw/Documents/ShellScript/grepDemo.sh --color
    }
    
    main

    其中test.txt的内容如下:

    one two three
    seven eight one eight three
    thirteen fourteen fifteen
     sixteen seventeen eighteen seven
    sixteen seventeen eighteen
            twenty seven
    one 504 one
    one 503 one
    one     504     one
    one     504 one
    #comment UP
    twentyseven
            #comment down
    twenty1
    twenty3
    twenty5
    twenty7

    Reference:

    grep是什么?怎么用?http://blog.jobbole.com/75410/#comment-61666

  • 相关阅读:
    Vue--运行项目发送http://localhost:8080/sockjs-node/info请求报错,造成浏览器不能热更新
    Vue笔记--同局域网下访问本地项目
    Vue笔记--通过自定义指令实现按钮操作权限
    css/css3实现未知宽高元素的垂直居中和水平居中
    【转载】Vue路由history模式踩坑记录:nginx配置解决404问题
    给动态生成的input框,添加readonly属性
    layui-form下隐藏元素的验证问题
    layui的省市县三级联动
    webstorm-激活码
    采坑
  • 原文地址:https://www.cnblogs.com/lxw0109/p/grep-demo.html
Copyright © 2011-2022 走看看