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

  • 相关阅读:
    #公式与实现# Jacobi迭代 Gauss-Seidel迭代
    数据结构-C:二叉树的遍历
    c++
    Unix Systems Programming
    二进制文件读取写入(一)
    关于理论、模型与算法
    《计算机图形学与几何造型导论》读书笔记1
    petaPar培训文档
    等参元的高斯积分详解
    水平集函数具体实现
  • 原文地址:https://www.cnblogs.com/lxw0109/p/grep-demo.html
Copyright © 2011-2022 走看看