Linux Sheel Programming Learning Note (ii)---grep command

Source: Internet
Author: User
Tags character classes control characters posix

The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.

grep works in such a way that it searches for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as filenames. The results of the search are sent to standard output without affecting the contents of the original file.

grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can use these return values to do some automated text processing work.

1. Command format:

grep [option] Pattern file

2. Command function:

A specific character used for filtering/searching. The use of regular expressions can be used in conjunction with a variety of commands, the use of very flexible.

3. Command parameters:

-a< Displays rows >--after-context=< displays the number of rows > #除了显示符合范本样式的那一列之外, and displays the contents after that line.

-B--byte-offset #在显示匹配行距文件头部的偏移量.

-O--byte-offset #与-B with matching words offset from the head of the file

-b< Displays rows >--before-context=< displays the number of rows > #除了显示符合样式的那一行之外, and displays the contents before the line.

-C--count #计算符合样式的行数.

-c< Display rows >--context=< Displays the number of rows > or-< Displays the number of rows > #除了显示符合样式的那一行之外, and displays the contents before the line.

-E--extended-regexp #将样式为延伸的普通表示法来使用.

-F--fixed-regexp #将样式视为固定字符串的列表, that is, regular expressions are not supported.

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

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

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

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

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

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

-Q--quiet or--silent #不显示任何信息.

-R--recursive #此参数递归查找子目录.

-S--no-messages #不显示错误信息.

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

-V--version #显示版本信息.

-W--word-regexp #只显示全字匹配的行. That is, the pattern is not treated as a regular expression.

-X--line-regexp #只有整行内容与模式匹配时候 before output.

-y #此参数的效果和指定 the same as the "-i" parameter.

POSIX characters:

POSIX (The Portable Operating System Interface) adds special character classes to the character encodings in different countries, such as [: Alnum:] is another notation for [a-za-z0-9]. You can place them in the [] number to be a regular expression, such as [a-za-z0-9] or [[: Alnum:]]. grep under Linux supports POSIX character classes in addition to Fgrep.

[: 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)

5. Usage examples:

Example 1: Finding the specified process

Command:

Ps-ef|grep SVN

Output:

[Email protected] ~]# Ps-ef|grep SVN

Root 4943 1 0 Dec05? 00:00:00 svnserve-d-r/opt/svndata/grape/

Root 16867 16838 0 19:53 pts/0 00:00:00 grep svn

[Email protected] ~]#

Description

The first record is the process of finding out; the second result is the grep process itself, not the process you are really looking for.

Example 2: Find the specified number of processes

Command:

Ps-ef|grep svn-c

Ps-ef|grep-c SVN

Output:

[Email protected] ~]# Ps-ef|grep svn-c

2

[Email protected] ~]# ps-ef|grep-c SVN

2

[Email protected] ~]#

Description

Example 3: Search by reading keywords from a file

Command:

Cat Test.txt | Grep-f Test2.txt

Output:

[email protected] test]# cat Test.txt

Hnlinux

Peida.cnblogs.com

Ubuntu

Ubuntu Linux

Redhat

Redhat

LinuxMint

[email protected] test]# cat Test2.txt

Linux

Redhat

[email protected] test]# Cat Test.txt | Grep-f Test2.txt

Hnlinux

Ubuntu Linux

Redhat

LinuxMint

[Email protected] test]#

Description

The output Test.txt file contains the content lines of the keywords read out from the Test2.txt file

Example 3: Reading a keyword from a file to search for and displaying line numbers

Command:

Cat Test.txt | GREP-NF Test2.txt

Output:

[email protected] test]# cat Test.txt

Hnlinux

Peida.cnblogs.com

Ubuntu

Ubuntu Linux

Redhat

Redhat

LinuxMint

[email protected] test]# cat Test2.txt

Linux

Redhat

[email protected] test]# Cat Test.txt | GREP-NF Test2.txt

1:hnlinux

4:ubuntu Linux

6:redhat

7:linuxmint

[Email protected] test]#

Description

The output Test.txt file contains the line of contents of the keyword read from the test2.txt file and displays the line number of each line

Example 5: Finding keywords from a file

Command:

grep ' Linux ' test.txt

Output:

[[email protected] test]# grep ' Linux ' test.txt

Hnlinux

Ubuntu Linux

LinuxMint

[[email protected] test]# grep-n ' Linux ' test.txt

1:hnlinux

4:ubuntu Linux

7:linuxmint

[Email protected] test]#

Description

Example 6: Find keywords from multiple files

Command:

grep ' Linux ' Test.txt test2.txt

Output:

[[email protected] test]# grep-n ' Linux ' Test.txt test2.txt

Test.txt:1:hnlinux

Test.txt:4:ubuntu Linux

Test.txt:7:linuxmint

Test2.txt:1:linux

[[email protected] test]# grep ' Linux ' Test.txt test2.txt

Test.txt:hnlinux

Test.txt:ubuntu Linux

Test.txt:linuxmint

Test2.txt:linux

[Email protected] test]#

Description

Multiple files, when the output of the query to the information content line, the name of the file will be the first line output and ":" as the identifier

Instance 7:grep does not show itself process

Command:

PS Aux|grep \[s]sh

PS aux | grep SSH | Grep-v "grep"

Output:

[[Email protected] test]# PS aux|grep ssh

Root 2720 0.0 0.0 62656 1212? Ss Nov02 0:00/usr/sbin/sshd

Root 16834 0.0 0.0 88088 3288? Ss 19:53 0:00 sshd: [Email protected]/0

Root 16901 0.0 0.0 61180 764 pts/0 s+ 20:31 0:00 grep ssh

[[Email protected] test]# PS Aux|grep \[s]sh]

[Email protected] test]# PS aux|grep \[s]sh

Root 2720 0.0 0.0 62656 1212? Ss Nov02 0:00/usr/sbin/sshd

Root 16834 0.0 0.0 88088 3288? Ss 19:53 0:00 sshd: [Email protected]/0

[[Email protected] test]# PS aux | grep SSH | Grep-v "grep"

Root 2720 0.0 0.0 62656 1212? Ss Nov02 0:00/usr/sbin/sshd

Root 16834 0.0 0.0 88088 3288? Ss 19:53 0:00 sshd: [Email protected]/0

Description

Example 8: Find the line content beginning with u

Command:

Cat Test.txt |grep ^u

Output:

[email protected] test]# cat test.txt |grep ^u

Ubuntu

Ubuntu Linux

[Email protected] test]#

Description

Example 9: Output line content that does not start with u

Command:

Cat Test.txt |grep ^[^u]

Output:

[email protected] test]# cat test.txt |grep ^[^u]

Hnlinux

Peida.cnblogs.com

Redhat

Redhat

LinuxMint

[Email protected] test]#

Description

Example 10: Output line content ending in hat

Command:

Cat Test.txt |grep hat$

Output:

[email protected] test]# cat test.txt |grep hat$

Redhat

Redhat

[Email protected] test]#

Description

Example 11:

Command:

Output:

[Email protected] test]# ifconfig eth0|grep "[0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\} "

inet addr:192.168.120.204 bcast:192.168.120.255 mask:255.255.255.0

[[email protected] test]# ifconfig eth0|grep-e "([0-9]{1,3}\.) {3} [0-9] "

inet addr:192.168.120.204 bcast:192.168.120.255 mask:255.255.255.0

[Email protected] test]#

Description

Example 12: Displaying a content line containing an ED or at character

Command:

Cat Test.txt |grep-e "Ed|at"

Output:

[email protected] test]# cat test.txt |grep-e "peida|com"

Peida.cnblogs.com

[email protected] test]# cat test.txt |grep-e "Ed|at"

Redhat

Redhat

[Email protected] test]#

Description

Example 13: Displays all rows in the current directory that contain at least 7 consecutive lowercase characters for each string in a file ending in. txt

Command:

grep ' [a-z]\{7\} ' *.txt

Output:

[[email protected] test]# grep ' [a-z]\{7\} ' *.txt

Test.txt:hnlinux

Test.txt:peida.cnblogs.com

Test.txt:linuxmint

[Email protected] test]#

Linux Sheel Programming Learning Note (ii)---grep command

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.