Linux Learning Notes---grep

Source: Internet
Author: User

Say grep first (search filter)

1. Command format:

grep [option] Pattern file

2. Command parameters:

-A Do not ignore binary data

-a< displays the number of rows > displays the number of rows, in addition to displaying the column that conforms to the template style, and displays the contents after that line

-B marks the number of the first character of the line before displaying the line that matches the style

-B Displays the number of rows in addition to the line that matches the style, and displays the contents before the line

-C calculates the number of columns that match the style

-c< displays the number of rows >, in addition to displaying the line that matches the style, and displays the contents before the row

-D < action > You must use this parameter when you specify that you want to find a directory instead of a file, otherwise the GREP directive returns information and stops the action

-e< template Style > Specify string as a style for finding the contents of a file

-E uses the style as an extended normal notation.

-f< Rule File > Specify a rule file with one or more rule styles, and grep finds the file content that matches the rule condition, in a regular style per line

-F #将样式视为固定字符串的列表

-G uses the style as a normal representation

-H #在显示符合样式的那一行之前, does not indicate the name of the file to which the line belongs.

-H #在显示符合样式的那一行之前 that represents the name of the file to which the row belongs.

-I. #忽略字符大小写的差别.

-L #列出文件内容符合指定的样式的文件名称.

-L #列出文件内容不符合指定的样式的文件名称.

-N #在显示符合样式的那一行之前, indicating the number of columns in the row.

-Q #不显示任何信息.

-R #此参数的效果和指定 the same as the "-D recurse" parameter.

-S #不显示错误信息.

-V #显示不包含匹配文本的所有行.

-V #显示版本信息.

-W #只显示全字符合的列.

-X #只显示全列符合的列.

-Y #此参数的效果和指定 "-i" parameter same

Regular Expressions for 3.grep:

^ #锚定行的开始

$ #锚定行的结束

. #匹配一个非换行符的字符

* #匹配零个或多个先前字符

. * #一起用代表任意字符

[] #匹配一个指定范围内的字符

[^] #匹配一个不在指定范围内的字符

\(.. \) #标记匹配字符

\< #锚定单词的开始

\> #锚定单词的结束

X\{m\} #重复字符x, M times

X\{m,\} #重复字符x, at least m times

X\{m,n\} #重复字符x, at least m times, not more than n times

\w #匹配字母和数字字符

\w #\w in reverse form, matching one or more non-word characters

\b #单词锁定符

[: Alnum:] #文字数字字符

[: Alpha:] #文字字符

[:d igit:] #数字字符

[: Graph:] #非空字符 (non-whitespace, control characters)

[: Lower:] #小写字符

[: Cntrl:] #控制字符

[:p rint:] #非空字符 (including spaces)

[:p UNCT:] #标点符号

[: Space:] #所有空白字符 (New line, Space, TAB)

[: Upper:] #大写字符

[: Xdigit:] #十六进制数字 (0-9,A-F,A-F)

eg

Find specified process: Ps-ef|grep svn

Find specified number of processes: Ps-ef|grep svn-c

Search by reading keywords from file: Output Test.txt file containing the contents of the keywords read out from the Test2.txt file Cat Test.txt | Grep-f Test2.txt

Search by reading keywords from a file and displaying line numbers: Cat Test.txt | GREP-NF Test2.txt

Find keywords from file: grep ' Linux ' test.txt grep-n ' Linux ' test.txt display line numbers

Find keywords from multiple files: grep ' Linux ' Test.txt test2.txt

grep does not show itself process: PS aux|grep ssh ps aux | grep SSH | Grep-v "grep"

Find the line that has the beginning of U: Cat test.txt | grep ^u

Output non-U-start line contents: Cat Test.txt |grep ^[^u]

Output IP Address: ifconfig eth0 | Grep-e "([0-9]{1,3}\.) {3} [0-9] "

Displays the content line that contains the ED or at character: Cat test.txt |grep-e "Ed|at"

Displays all the lines in the current directory that contain a string of at least 7 consecutive lowercase characters in a file ending with. txt: grep ' [a-z]\{7\} ' *.txt or grep-e ' [a-z]{7} ' Test.txt /c0>

Log file too large, not good to see, we want to see what we want, or get the same kind of data, such as no 404 log Information: grep '. ' Access1.log | Grep-ev ' 404 ' > Access2.log

grep '. ' Access1.log|grep-ev ' 404 ' > Access2.log

grep '. ' Access1.log|grep-ev ' (404|/photo/|/css/) ' > Access2.log

grep '. ' Access1.log|grep-e ' 404 ' > Access2.log

The first two sentences of the above 3 commands are to find the Access1.log file in the current directory, find those lines that do not contain 404, put them in Access2.log, and then remove the ' V ', which is to put 404 rows into Access2.log

awk match numbers cannot be used \d

Linux Learning Notes---grep

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.