zoukankan      html  css  js  c++  java
  • Searching for a String in Multiple Files

    常用命令:

    grep -r -n -B1 -A1 --include=*.c strstr *
    findstr /S "MB_ICONHAND" *.h
     
    Ever need to search through all your files for a certain word or phrase? I did, and to make matters more complicated, the files were all in different sub-directories. A quick Google search turned up a few scripts involving multiple commands and temporary files. Then I found a simpler solution.

    If you're a Unix/Linux/BSD user, you probably know about the grep command, but did you know it's recursive? That's right, grep can search through your entire directory tree, scanning every file along the way. It will even return the results in multiple formats!

    Here's an example. In this case we're searching for the word "modules":

    grep -r "modules" .

    By using the "-r" switch, we're telling grep to scan files in the current directory and all sub-directories. It will return a list of files the string was found in, and a copy of the line it was found on.

    If you'd rather just get the file names and skip the rest of the output, use the "-l" switch, like so:

    grep -lr "modules" .

    Here's another tip: grep also supports regular expressions, so matching against a wildcard pattern is easy:

    grep -lr "mod.*" .

    That command will print a list of files containing any word starting with "mod".

    You can also use grep to search for multiple words:

    grep -r "drupal\|joomla\|wordpress" .

    And, of course, grep supports file name wildcards in the standard unix fashion. In this example, grep will search only file names starting with "log":

    grep -lr "mod.*" ./log*

    Unfortunately, not all versions of grep support recursive searching, and some use different switches, so if you're running into problems, check your man pages:

    man grep

    All commands listed in this tutorial have been tested on the latest version of FreeBSD.

    If you want to learn more about Grep, check out the Grep Pocket Reference!

    Posted by John on 2008-02-05




    FINDSTR 使用说明

    FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
            [/C:string] [/G:file] [/D:dir list][/A:color attributes] [/OFF[LINE]]
            strings [[drive:][path]filename[...]]

     在行头或是行尾匹配模式
      /B         Matches pattern if atthe beginning of a line.
      /E         Matches pattern if atthe end of a line.

      /L         Uses search stringsliterally.
      使用正则表达式进行搜索
      /R        Uses search strings as regular expressions.
      搜索当前目录及所有子目录
      /S         Searches for matchingfiles in the current directory and all
                subdirectories.
      不区分大小写
      /I        Specifies that the search is not to be case-sensitive.
      输出行
      /X        Prints lines that match exactly.
      /V         Prints only linesthat do not contain a match.
      输出行号
      /N        Prints the line number before each line that matches.
      如果文件包含了一个匹配,就只输出文件名
      /M         Prints only thefilename if a file contains a match.
      /O         Prints characteroffset before each matching line.
      /P         Skip files withnon-printable characters.
      /OFF[LINE] Do not skip files with offline attribute set.
      /A:attr    Specifies color attribute with two hex digits.See "color /?"
      /F:file    Reads file list from the specified file(/stands for console).
      /C:string  Uses specified string as a literal search string.
      /G:file    Gets search strings from the specified file(/stands for console).
      /D:dir     Search a semicolon delimited list ofdirectories
      strings    Text to be searched for.
      [drive:][path]filename
                Specifies a file or files to search.

    Use spaces to separate multiple search strings unless the argument is prefixed
    with /C.  For example, 'FINDSTR "hello there" x.y' searches for"hello" or
    "there" in file x.y.  'FINDSTR /C:"hello there" x.y'searches for
    "hello there" in file x.y.

    Regular expression quick reference:
      .        Wildcard: any character
      *        Repeat: zero or moreoccurrences of previous character or class
      ^        Line position: beginning ofline
      $        Line position: end of line
      [class]  Character class: any one character in set
      [^class] Inverse class: any one character not in set
      [x-y]    Range: any characters within the specified range
      \x       Escape: literal use ofmetacharacter x
      \<xyz    Word position: beginning of word
      xyz\>    Word position: end of word

    For full information on FINDSTR regular expressions refer to the online Command
    Reference.



  • 相关阅读:
    MVC3、如何应用EntityFramework 连接MySql 数据库 Kevin
    DEV EXPRESS Summary Footer 不显示 Kevin
    装饰模式 Kevin
    Dev 控件 GridControl 控件 二次绑定数据源的问题。 Kevin
    System.InvalidOperationException 异常 Kevin
    LINQ to XML Kevin
    代理模式——代码版“吊丝的故事” Kevin
    VS2012 中的设备 面板 Kevin
    maven 学习笔记(三)创建一个较复杂的 eclipse+android+maven 工程
    maven 学习笔记(一)eclipse+android+maven
  • 原文地址:https://www.cnblogs.com/IS2120/p/6746026.html
Copyright © 2011-2022 走看看