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

  • 相关阅读:
    常见算法的时间复杂度
    electron 展示pdf
    AudioContext
    js 计算文字宽度
    python 窗口被关闭报错
    qq行情数据。sina行情JOSN,建议用qq,涨跌,财务数据有-市盈
    python AttributeError: 'NoneType' object has no attribute 'prepare'
    策略日志
    使用Python下载A股行情的几种方法
    easytrader
  • 原文地址:https://www.cnblogs.com/lxw0109/p/grep-demo.html
Copyright © 2011-2022 走看看