Linux Basic command grep egrep fgrep usage and regular expressions

Source: Internet
Author: User
Tags character classes control characters grep regular expression posix egrep

1.1, the basic definition: www.2cto.com

grep (Global search regular RE) and print out of the line, full search of regular expressions and print out the lines, is a powerful text search tool that can only use basic regular expressions to search for text and print matching lines.

grep is a common and commonly used command, and its main function is to compare string data and then print out strings that match the user's needs, but the idea is that when grep looks for a string in the data, the data is filtered in the "Whole row" unit.

The Egrep command is equivalent to GREP-E, which allows you to search for text using an extended regular expression and print a string that meets the user's needs.

The Fgrep command is equivalent to Grep-f, which uses a fixed string to search for text, but does not support a reference to a regular expression, so the command executes the fastest.

1.2. Basic usage of commands

grep [option] ' Search string ' filename

grep Common options:

-A: Search for data in a binary file as a text file

-C: Count the number of ' search strings ' found

-I: Ignore case

-V: Reverse lookup, which shows the line without the ' search string ' content

-O: Show only strings that are pattern-matched

-N: Output line number

--colour (color): Colour display

-A: Shows the following n lines matching to the word Funa

-B: Displays the first n rows matched to the word Funa

-C: Displays the front and back n lines matching to the word Funa

Second, the regular expression

2.1, the basic definition:

Regular expressions use a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern. In short, a regular expression is a method of handling strings, the processing of strings in behavioral units, and the assistance of special symbols that allow users to easily search/replace a particular string.

Regular expressions fall into two categories: basic regular expressions and extended regular expressions.

2.2. Detailed description of regular expressions

2.2.1, the basic regular expression:

(1) meta-characters:

. : Matches any single character

FG: Finding a line that contains student and student with one character behind it

grep ' student. '/etc/passwd (patterns can be quoted in single and double quotes, and double-cited if the variable is to be substituted in the pattern)

[]: matches any single character in the specified range [abc],[a-z],[0-9],[a-za-z]

FG: Finding rows with numbers

grep ' [0-9] '/etc/passwd

[^]: matches any single character outside the specified range

FG: Finds lines that do not have lowercase letters.

grep ' [^a-z] '/etc/inittab

[: Space:]: denotes white space characters

[:p UNCT:]: Represents the collection of all punctuation marks

[: Lower:]: denotes all lowercase letters

[: Upper:]: denotes all uppercase letters

[: Alpha:]: Indicates uppercase and lowercase letters

[:d igit:]: Indicates the number of children

[: Alnum:]: denotes numbers and uppercase letters-----use format [[: Alnum:]] etc.

(2) Number of matches:

*: Matches any of its preceding characters any time

FG: Find root appears 0 or more rows or more than 0 times

grep ' root* '/etc/passwd

. *: Any character

FG: Finding rows that contain root

grep ' root.* '/etc/passwd

\?: Match its preceding character 1 or 0 times

\{m,n\}: Match its first character at least m, up to N times)

(3) Character anchoring:

^: Anchor the beginning of the line, any content after this character must appear at the beginning of the line

FG: Finding lines beginning with # at the beginning of a line

grep ' ^# '/etc/inittab

$: Anchor line end, any content in front of this character must appear at the end of the row

FG: Finding rows at the beginning of the line that end with Root

grep ' root$ '/etc/inittab

^$: Anchor blank lines, you can count blank lines

\< or \b: Anchor word, any character following it must be the first occurrence of the word

FG: A row that finds root and does not contain any characters before root

grep ' \<root '/etc/man.config

\> or \b: anchor ending, any character in front of it must be a word trailing fg:\<root\> find root word grep "\<root\>" =grep "\broot\ b

2.2.2, extended Regular expression:

The extended regular expression only makes a minor modification to the basic regular expression, which is modified as follows:

In the extended regular expression, the \ (\) is written (), \{\} as {}, plus the +: number of matches, matching the preceding characters appear at least once, no upper limit, |: Or (two take one), the rest are the same, the basic regular expression, using () {}.? | All need to be escaped, not required in extended regular expressions To add \, its details are as follows:

(1) The command and usage of character matching is the same as the usage of the basic regular expression, which is not repeated here.

(2) Number of matches:

*: Matches any of its preceding characters

? : matches 0 of its preceding characters or 1 of this

+: Match its preceding character at least 1 this

FG: At least one blank character: ' [[: space:]]+ '

{M,n}: matches its preceding characters m to n times

(3) The usage of the character anchor is the same as that of the basic regular expression, and is not elaborated here.

(4) Special characters:

| : The meaning of a representative or.

Fg:grep-e ' c|cat ' file: Indicates that you are looking for C or cat

\.:\ Represents the escape character, which represents the symbol.

The grep command takes advantage of small instances

(1) Displays the line with # beginning with/etc/inittab, followed by one or more whitespace characters, and followed by any non-whitespace character

grep ' #[[:space:]]*[^[:space:]] '/etc/inittab

(2) output is not a digital switch line grep ' ^[^0-9] '

/etc/passwd

(3) Output header is 1 or 2

grep ' ^\ (1\|2\) '/etc/inittab

Or

Grep-e ' ^ (1|2) '/etc/inittab

(4) Find the front is the RC in the indirect any character followed by/RC

grep '. *\ (rc\). *\/\1.* '/etc/inittab

(5) Remove the IP from the current computer

Ifconfig |grep-a 1 "^eth0" |grep "\<[0-9. \{1,\} |cut-d:-f2

(6) Find information about the user account on the current system named student (must appear at the beginning of the line), the file is/etc/passwd

grep "^student"/etc/passwd

2. grep regular expression meta-character set (base set)
^

The start of the anchoring line is as follows: ' ^grep ' matches all lines that begin with grep.

$

The end of the anchoring line is as follows: ' grep$ ' matches all rows ending with grep.

.

Match a non-newline character such as: ' GR.P ' matches gr followed by an arbitrary character followed by P.

*

Match 0 or more previous characters such as: ' *grep ' matches all one or more spaces followed by the grep line. * Use together to represent any character.

[]

Matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep.

[^]

Matches a character that is not within the specified range, such as: ' [^a-fh-z]rep ' matches a letter that does not contain a-r and t-z, immediately following the line of the Rep.

\(.. \)

Tag matching characters, such as ' \ (love\) ', Love is marked as 1.

\<

Anchors the beginning of a word, such as: ' \<grep ' matches a line containing a word that begins with grep.

\>

Anchors the end of a word, such as ' grep\> ', that matches a line containing a word that ends with grep.

X\{m\}

Repeat characters x,m times, such as: ' 0\{5\} ' matches rows containing 5 O.

X\{m,\}

Repeat the character X, at least m times, such as: ' O\{5,\} ' matches at least 5 rows of O.

X\{m,n\}

Repeat the character X, at least m times, not more than n times, such as: ' O\{5,10\} ' matches the line of 5--10 O.

\w

Match literal and numeric characters, that is, [a-za-z0-9], such as: ' G\w*p ' matches with G followed by 0 or more literal or numeric characters, followed by P.

\w

\w the inverse of the form, matching one or more non-word characters, such as the dot period and so on.

\b

The word lock, such as: ' \bgrep\b ' only matches grep.

3. Meta-character expansion set for Egrep and GREP-E
+

Matches one or more previous characters. such as: ' [a-z]+able ', matching one or more lowercase letters followed by able strings, such as loveable,enable,disable, etc.

?

Matches 0 or more previous characters. such as: ' Gr?p ' matches the GR followed by one or no characters, then the line of P.

A|b|c

Match A or B or C. such as: grep|sed matching grep or sed

()

Grouping symbols, such as: Love (able|rs) ov+ match loveable or lovers, matching one or more ov.

X{m},x{m,},x{m,n}

function with x\{m\},x\{m,\},x\{m,n\}

4. POSIX character class

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:]

Literal numeric characters

[: Alpha:]

Literal characters

[:d Igit:]

numeric characters

[: Graph:]

Non-null characters (non-whitespace, control characters)

[: Lower:]

lowercase characters

[: Cntrl:]

Control characters

[:p rint:]

Non-null characters (including spaces)

[:p UNCT:]

Punctuation

[: Space:]

All whitespace characters (new lines, spaces, tabs)

[: Upper:]

Uppercase characters

[: Xdigit:]

hexadecimal digits (0-9,a-f,a-f)

5. grep command Options
-?

Show matching rows up and down at the same time? Line, such as: grep-2 pattern filename Displays the top and bottom 2 rows of a matching row.

-b,--byte-offset

Prints the block number where the line is printed before the matching line.

-C,--Count

Only the number of matched rows is printed and the matching content is not displayed.

-F File,--file=file

Extract the template from the file. An empty file contains 0 templates, so nothing matches.

-h,--no-filename

When searching multiple files, the matching file name prefix is not displayed.

-i,--ignore-case

ignores case differences.

-q,--quiet

Cancels the display, returning only the exit status. 0 indicates that a matching row was found.

-l,--files-with-matches

Prints a list of files that match the template.

-l,--files-without-match

Prints a list of files that do not match the template.

-n,--line-number

Prints the line number in front of the matching line.

-s,--silent

does not display an error message about a file that does not exist or cannot be read.

-v,--revert-match

Anti-retrieval, displaying only rows that do not match.

-w,--word-regexp

If referenced by \< and \>, the expression is searched as a single word.

-v,--version

Displays software version information.

6. Example

To use a good grep this tool, in fact, is to write a regular expression, so here does not have all the functions of grep to explain, only a few examples, explain a regular expression of the wording.

$ ls-l | grep ' ^a '

Filters the contents of the Ls-l output through a pipeline, displaying only the lines that begin with a.

$ grep ' test ' d*

Displays all rows that contain test in a file that begins with D.

$ grep ' test ' AA bb cc

Displays the line that matches test in the aa,bb,cc file.

$ grep ' [a-z]\{5\} ' AA

Displays all rows that contain a string of at least 5 consecutive lowercase characters for each string.

$ grep ' w\ (es\) t.*\1 ' AA

If West is matched, es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (\1), which is found to display the row. If you use Egrep or GREP-E, you do not have to escape the "\" number, directly written as ' W (es) t.*\1 ' on it.

1. Parameters:
-I: Ignore case
-C: Print the number of matching rows
-L: Find included matches from multiple files
-V: Find rows that do not contain matches
-N: Print row and row labels that contain matches
2, RE (regular expression)
\ ignore the original meaning of special characters in regular expressions
^ matches the start line of the regular expression
$ matches the end line of a regular expression
\< starting from the line that matches the regular expression
\> to the end of the line that matches the regular expression
[] A single character, such as [a] that a meets the requirements
[-] range, such as [A-z], i.e. a,b,c to Z all meet the requirements
. All the individual characters
* All characters, length can be 0
3. Example
# Ps-ef | grep in.telnetd
Root 19955 181 0 13:43:53? 0:00 in.telnetd
# more Size.txt The contents of the size file
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
A013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# More Size.txt | grep ' [A-b] ' range, such as [A-z], i.e. a,b,c to Z all meet the requirements
b124230
b034325
a081016
a022021
a061048
b103303
A013386
b044525
# More Size.txt | grep ' [A-b] ' *
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
A013386
b044525
m8987131
B081016
M45678
B103303
BADc2345
# More Size.txt | grep ' [b] ' single character; if [a] is a compliance requirement
b124230
b034325
b103303
b044525
# More Size.txt | grep ' [BB] '
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345
# grep ' Root '/etc/group
Root::0:root
Bin::2:root,bin,daemon
Sys::3:root,bin,sys,adm
Adm::4:root,adm,daemon
Uucp::5:root,uucp
Mail::6:root
Tty::7:root,tty,adm
Lp::8:root,lp,adm
Nuucp::9:root,nuucp
Daemon::12:root,daemon
# grep ' ^root '/etc/group matches the start line of the regular expression
Root::0:root
# grep ' UUCP '/etc/group
Uucp::5:root,uucp
Nuucp::9:root,nuucp
# grep ' \&LT;UUCP '/etc/group
Uucp::5:root,uucp
# grep ' root$ '/etc/group matches the end line of the regular expression
Root::0:root
Mail::6:root
# More Size.txt | Grep-i ' B1. '-I: Ignore case
b124230
b103303
B103303
# More Size.txt | Grep-iv ' B1. '-V: Find rows that do not contain matches
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
A013386
b044525
m8987131
B081016
M45678
BADc2345
# More Size.txt | Grep-in ' B1. "
1:b124230
9:b103303
15:b103303
# grep ' $ '/etc/init.d/nfs.server | Wc-l
128
# grep ' \$ '/etc/init.d/nfs.server | Wc–l ignores the original meaning of special characters in regular expressions
15
# grep ' \$ '/etc/init.d/nfs.server
Case "$" in
>/tmp/sharetab.$$
["x$fstype"! = Xnfs] &&
echo "$path \t$res\t$fstype\t$opts\t$desc"
>>/tmp/sharetab.$$
/usr/bin/touch-r/etc/dfs/sharetab/tmp/sharetab.$$
/usr/bin/mv-f/tmp/sharetab.$$/etc/dfs/sharetab
If [-f/etc/dfs/dfstab] &&/usr/bin/egrep-v ' ^[]* (#|$) '
If [$STARTNFSD-eq 0-a-f/etc/rmmount.conf] &&
If [$startnfsd-ne 0]; Then
elif [!-n "$_init_run_level"]; Then
While [$wtime-GT 0]; Do
Wtime= ' Expr $wtime-1 '
If [$wtime-eq 0]; Then
echo "Usage: $ {Start | Stop} "
# more Size.txt
The test file
Their is files
The end
# grep ' the ' size.txt
The test file
Their is files
# grep ' \<the ' size.txt
The test file
Their is files
# grep ' the\> ' size.txt
The test file
# grep ' \<the\> ' size.txt
The test file
# grep ' \<[tt]he\> ' size.txt
The test file
The end

Linux Basic command grep egrep fgrep usage and regular expressions

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.