grep command to find the file that contains the contents of a

effect 

Linux systems grep command is a powerful text search tool, it can use a regular expression search text, and print out the matching rows. grep stands for Global Regular Expression Print, represents the global regular expression version, its usage rights for all users.
grep family, including grep, egrep and fgrep. egrep and fgrep grep command with only a very small difference. grep egrep is extended to support more re metacharacters, fgrep is fixed grep or fast grep, they put all the letters as words, that is, regular expression meta-character representation back to the literal meaning of its own It is no longer special. linux using the GNU version of grep. It is more powerful and can be used by egrep and fgrep functionality -G, -E, -F command line option.

The main parameters and format

    grep [options]
    The main parameters: grep --help to see
         -c: Only output count of matching lines.
        -i: not case sensitive.
        -h: do not display the file name when querying multiple files.
        -l: only the output file name contains characters match the query multiple files.
        -n: display matching lines and line number.
        -s: do not display an error message does not exist or no match text.
        -v: Displays all lines matching text.
        --color = auto: Keywords section can be found add display color.


    regular expression pattern main parameters:
    \: Ignore the original meaning of the regular expression special characters.
    ^: Match the regular expression of the start line.
    $: The end of the line matches the regular expression.
    \ <: Beginning match the regular expression.
    \>: The line to match the regular expression ends.
    []: A single character, such as [A] i.e. A compliance.
    [-]: range, such as [AZ], i.e. A, B, C until Z meet the requirements.
    .: All single character.
    *: All characters in length can be zero. 
   x\{m\}  #重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。   
   x\{m,\} #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。   
   x\{m,n\}#重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。  
 

 Examples

Display all lines in a file at the beginning of the test contained in d

grep 'test' d *

Display line test match in aa, bb, cc file

grep 'test' aa bb cc

Show all rows each string comprising at least 5 contiguous string of lower case characters

grep [AZ] \ {5 \} 'aa

\ Represents the escape character

Example grep command complex

    Clear requirements Search subdirectories:
    grep -r
    Or ignore subdirectories
    grep -d ships
    If you have a lot of output, you can pipe it to the 'less' read:
    itcast$ grep magic /usr/src/Linux/Documentation/* | less
    This way, you can more easily read.
    One thing to note, you must provide a file filtering (then search all files with *). If you forget, 'grep' will always wait until the program is interrupted. If you encounter such a case, press, and then try again.
    Here are some interesting command line parameters:
    grep -i pattern files: case-insensitive search. By default case-sensitive,
    grep -l pattern files: lists only the matching file names,
    grep -L pattern files: list of file names do not match,
    grep -w pattern files: only whole words, rather than part of the string (such as matching 'magic', rather than 'magical'),
    grep -C number pattern files: context matches are shown [number] lines,
    grep pattern1 | pattern2 files: display of matching lines pattern1 or pattern2,
    For example: grep "abc \ | xyz" testfile line represents the filter containing the abc or xyz
    grep pattern1 files | grep pattern2 :显示既匹配 pattern1 又匹配 pattern2 的行。
    grep -n pattern files 即可显示行号信息
    grep -c pattern files 即可查找总行数
    还有些用于搜索的特殊符号:\< 和 \> 分别标注单词的开始与结尾。
    例如:
    grep man * 会匹配 ‘Batman’、’manic’、’man’等,
    grep ‘\<man’ * 匹配’manic’和’man’,但不是’Batman’,
    grep ‘\<man\>’ 只匹配’man’,而不是’Batman’或’manic’等其他的字符串。

  

 

Guess you like

Origin www.cnblogs.com/joanna123/p/12182239.html