Linux system commands the Three Musketeers grep

Source: Internet
Author: User
Tags egrep

Command name: grep

Command function:

Text Lookup or search tool

Detailed Description:

You can also search for text with regular expressions and print out the matching rows

can also be used to filter and search specific strings, using very flexible

Common parameters:

-A #不要忽略二进制数据

-A #除了显示符合范本样式的那一行之外 and displays the contents after the line

-B #在显示符合范本样式的那一行之外 and displays the contents before the line

-B #除了显示符合样式的那一行之外 and displays the contents before the line

-C #计算符合范本样式的列数

-C #除了显示符合范本样式的那一列之外 and displays the contents before the column

-D #当指定要查找的是目录而非文件时, this parameter must be used, otherwise the grep command will return information and stop the action

-E #指定字符串作为查找文件内容的范本样式

-e #将范本样式为延伸的普通表示法来使用, meaning the use of extended regular expressions can be used

-F #指定范本文件, whose contents have one or more template styles, let grep find the file content that conforms to the template criteria, in the format of the template style for each column

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

-G #将范本样式视为普通的表示法来使用

-H #在显示符合范本样式的那一列之前, does not indicate the name of the file to which the column belongs

-H #在显示符合范本样式的那一列之前, indicating the file name of the column

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

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

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

-N #在显示符合范本样式的那一列之前, indicating the number of the column

-Q #不显示任何信息

-r/-r #此参数的效果和指定 "-D recurse" parameters are the same

-S #不显示错误信息

-V #反转查找

-V #显示版本信息

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

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

-Y #此参数效果跟 "-i" same

-O #只输出文件中匹配到的部分

Regular expressions

^ #匹配以XX开头的行

$ #匹配以XX结尾的行

Common examples:

#在多个文件中查找:

grep "File" File_1 file_2 file_3


#输出除之外的所有行-V Option:

Grep-v "File" file_name


#标记匹配颜色--color=auto Options:

grep "File" file_name--color=auto


#使用正则表达式-E option:

Grep-e "[1-9]+"

Or

Egrep "[1-9]+"


#只输出文件中匹配到的部分-o Option:

Echo this was a test line. | Grep-o-E "[A-z]+\."]

Line.

Echo this was a test line. | Egrep-o "[A-z]+\.]

Line.


#统计文件或者文本中包含匹配字符串的行数-C Option:

Grep-c "Text" file_name

2


#输出包含匹配字符串的行数-N option:

grep "Text"-N file_name

Or

Cat file_name | grep "Text"-N


#多个文件

grep "Text"-N file_1 file_2


#打印样式匹配所位于的字符或字节偏移:

echo gun is not Unix | Grep-b-O "not"

7:not

#一行中字符串的字符串是从该行的第一个字符开始计算, the starting value of 0,**-b-o** is always used in conjunction with


#搜索多个文件并查找匹配文本在哪些文件中:

Grep-l "text" File1 file2 file3 ...


#grep递归搜索文件

#在多级目录中对文本进行递归搜索:

grep "Text". -r-n


#忽略匹配样式中的字符大小写:

echo "Hello World" | Grep-i "HELLO"

Hello


#选项-e Specifies multiple matching styles:

Echo this was a text line | Grep-e "is"-e "line"-O

Is

Line


#也可以使用 the **-f** option to match multiple styles, write down the characters that need to be matched line by row in the style file.

Cat Patfile

Aaa

Bbb


Echo AAA BBB CCC DDD Eee | Grep-f Patfile-o

Include or exclude the specified file in the grep search results:

#只在目录中所有的. PHP and. html files recursively search for characters "main ()"

grep "Main ()". -R--include *. {php,html}


#在搜索结果中排除所有README文件

grep "Main ()". -R--exclude "README"


#在搜索结果中排除filelist文件列表里的文件

grep "Main ()". -R--exclude-from FileList

grep with Xargs with a 0-value byte suffix:

# test File:

echo "AAA" > File1

echo "BBB" > File2

echo "AAA" > File3


grep "AAA" file*-lz | xargs-0 RM


#执行后会删除file1和file3, the grep output uses the-Z option to specify a 0-value byte as the Terminator file name, xargs-0 reads the input and separates the file name with a value of 0 bytes Terminator, and then deletes the matching file,-Z is usually used in conjunction with-L.

grep Silent Output:

Grep-q "test" filename

# no information is output and if the command runs successfully returns 0, a non-0 value will be returned if it fails. Typically used for conditional testing.

Print a line before or after matching text:

# show 3 rows after matching a result, use the-a option:

Seq 10 | grep "5"-A 3

5

6

7

8


# displays 3 rows before matching a result, using the-B option:

Seq 10 | grep "5"-B 3

2

3

4

5


# Displays the first three rows and the last three rows that match a result, using the-C option:

Seq 10 | grep "5"-C 3

2

3

4

5

6

7

8


# If there are multiple matching results, the "--" will be used as the delimiter between each matching result:

Echo-e "A\NB\NC\NA\NB\NC" | grep a-a 1

A

B

--

A

B


#查询目录下所有文件数量

Ll-r/root/test |egrep "^[sl-][rwx-]{9}" | Wc-l

5


This article is from the "Migrant Brother blog" blog, please be sure to keep this source http://mingongge.blog.51cto.com/2429897/1981139

Linux system commands the Three Musketeers 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.