Use of GREP commands in Linux

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

Use of GREP commands in Linux

grep (Global search Regular expression (RE) and print out of the line, a comprehensive search for regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching rows. The grep family of Unix includes grep, Egrep, and Fgrep.

The grep command is a powerful text search tool that uses regular expressions to search for text and print matching rows. The grep full name is global Regular Expression Print, which represents the global regular expression version, and its use permissions are all users. Ps-ef | grep httpd: Check if httpd process exists

The Ps-aux |awk ' $2~/32651/' filter can be used Ps-aux |awk ' $2!~/32651/' grep 2567 to show all rows that appear in 2567 of this string; | This is a pipe that gives the output the result as input to the next command.

awk and SED are much more powerful than grep, and grep is generally slightly mentioned in Unix-speaking books, but awk and SED will focus.

Find files containing the string "wl0505" in the ETC Directory: find/etc-name "*" |xargs grep "wl0505" > ~/thefile

Grep-rn wl0505/etc/*

Find/-name "*.*" | Xargs grep "wl0505" >>/home/filename*.* is the filename and extension,>> to redirect the results to a file in the following path, not displayed on the terminal.

"*" indicates a line matching the character with * means to find the file with * and print the line found./-maxdepth 1 | grep "*" find./-maxdepth 1 | grep * These two commands are equivalent to grep, he only accepts regular expression matches *? These characters need to be added with an escape character

The Find in Linux differs from the grep command. The Find command is used to look up files in the directory tree that match the search criteria; The grep command is used to find the line containing the template text in the input stream. The Find command is used in conjunction with the grep command to further filter the search results

Matches all (recursively lookup) files that begin with ' # ' under the directory/cpl:

Ocs101:~/cpl # grep-r ' ^# '.

./FK.C: #include

./FK.C: #include

./flower2.c: #include

./flower2.c: #include

./RECURSIVE_FLOWER.C: #include

./RECURSIVE_FLOWER.C: #include

./RECURSIVE_FLOWER.C: #include

./RECURSIVE_FLOWER.C: #define M 8

./FLOWER.C: #include

./FLOWER.C: #include

./FLOWER.C: #include

./FLOWER.C: #define N 1e8

Matches all files (recursively lookup) ending with '} ' under directory/cpl:

Ocs101:~/cpl # grep-r ' ^} '.

./FK.C:}

./FK.C:}

./FK.C:}

./narcissus.java:}

./FLOWER2.C:}

./FLOWER2.C:}

./FLOWER2.C:}

./RECURSIVE_FLOWER.C:}

./RECURSIVE_FLOWER.C:}

./RECURSIVE_FLOWER.C:}

./RECURSIVE_FLOWER.C:}

./FLOWER.C:}

./FLOWER.C:}

./FLOWER.C:}

./FLOWER.C:}

./FLOWER.C:}

Expression set

^

The beginning of the anchoring line, such as: ' ^grep ' matches all rows beginning with grep.

$

The end of the anchoring line, such as: ' grep$ ', matches all rows that end with grep.

.

Match a newline character Furu: ' GR.P ' matches the GR followed by an arbitrary character followed by P.

*

Match 0 or more previous characters Furu: ' *grep ' (note * preceded by a space) matches all 0 or more spaces immediately following the grep row, you need to use Egrep or grep with the-e option ... * 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 in the specified range, such as: ' [^a-fh-z]rep ' match does not contain a letter beginning with a-f and H-z, followed by the rep line.

(..)

Tags match characters, such as ' (Love) ', and Love is marked as 1.

<

To anchor the beginning of a word, as: '

>

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

X{M}

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

X{m,}

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

X{m,n}

Repeat character X, at least m times, no more than n times, such as: ' o{5,10} ' matches 5--10 O's line.

W

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

W

The inverse form of w that matches one or more non word characters, such as the dot number period.

B

Word locks, such as: ' BGREPB ' only match grep. [1] meta-character extension set for Egrep and GREP-E

+

Matches one or more of the previous characters. such as: ' [a-z]+able ', match one or more lowercase letters followed by the able string, such as loveable,enable,disable.

?

Matches 0 or one previous character. For example: ' Gr?p ' matches a gr followed by one or no characters, then a line of P.

A|b|c

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

()

Group 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}

POSIX character classes

POSIX (The Portable operating System Interface) adds special character classes, such as [: Alnum:], to keep one to the character encodings in different countries. To put them inside the [] number, you can become regular expressions, such as [a-za-z0-9] or [[: Alnum:]]. Under Linux, grep supports the POSIX character classes except Fgrep.

[: Alnum:]

Literal numeric character

[: Alpha:]

Literal characters

[:d Igit:]

numeric characters

[: Graph:]

Non-null characters (not spaces, control characters)

[: Lower:]

lowercase characters

[: Cntrl:]

Control characters

[:p rint:]

Non-null characters (including spaces)

[:p UNCT:]

Punctuation

[: Space:]

All white-space characters (new lines, spaces, tabs)

[: Upper:]

Uppercase characters

[: Xdigit:]

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

Command options

-?

Also displays the rows from top to bottom of the matching row, such as: grep-2 pattern filename Displays the top and bottom 2 rows of matching rows at the same time.

-A,--text

Equivalent to matching text, for (Binary file (standard input) matches) error

-b,--byte-offset

Print the block number in which the line is printed before the matching line.

-C,--Count

Prints only the number of rows that match and does not display the matching content.

-F File,--file=file

Extracts the template from the file. The empty file contains 0 templates, so nothing matches.

-h,--no-filename

When searching for multiple files, the matching filename prefix is not displayed.

-i,--ignore-case

ignores case differences.

-q,--quiet

Suppresses display and returns only the exit status. 0 indicates that a matching row was found.

-l,--files-with-matches

Print a list of files that match the template.

-l,--files-without-match

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

-n,--line-number

Print the line number before the matching line.

-s,--silent

Does not display error messages about the absence or inability to read files.

-v,--revert-match

Reverse retrieve, showing only rows that do not match.

-w,--word-regexp

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

-R, R,--recursive

Recursively read all the files in the directory, including subdirectories. For example, grep-r ' pattern ' Test matches pattern in all files in test and its subdirectories.

-v,--version

Displays software version information.

Instance

To use a good grep this tool, in fact, is to write a good regular expression, so here is not the grep all the features of the example 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 pipe, showing only the rows that start with a.

$ grep ' test ' d*

Displays the rows that contain test in all files that start with D.

$ grep ' test ' AA bb cc

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

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

Displays all the lines that contain 5 consecutive lowercase characters for each string.

$ grep ' W (es) t.*1 ' AA

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

Attention

On some machines, it is possible to use the-e parameter to make a logical match (see below)

grep "A|b" (matches a line containing a character style of "a|b")

Grep-e "A|b" (matches a line that contains a character style of "a" or "B")

The description of the-e parameter inside the man grep is

-E

Treats each pattern specified as a extended regular expression (ERE). A NULL value for the ERE matches every

Line.

note:the grep command with THE-E flag are the same as the Egrep command, except that error and usage messages

are different and THE-S flag functions differently.

Outreach command

Egrep command to search for file acquisition mode.

The egrep command searches the input file (the default is standard input) for rows that match the pattern specified with the patterns parameter. These patterns are complete regular expressions, as in the ED command (except (backslashes) and (double backslashes)). The following rules apply to the Egrep command as well:

* A regular expression followed by A + (plus) matches one or more regular expressions.

* A regular expression with one behind it? (question mark) matches 0 or one of the regular expressions.

* by | (vertical bar) or multiple regular expressions separated by a newline character match a string that matches any of the regular expressions.

* A regular expression can be included in "()" (bracket) for grouping.

Line breaks will not be matched by regular expressions.

The precedence order of operators is [,], *,?, +, Merge, | and line breaks.

Note: The Egrep command is the same as the grep command with the-e flag, except that the error message and the usage message are different and the S-flag function is different.

The egrep command displays the file that contains the matching row, if you specify more than one.

Characters that have special meaning to the shell ($, *, [, |, ^, (,),) must be enclosed in double quotes when they appear in the pattern parameter. If the pattern parameter is not a simple string, it is usually necessary to enclose the entire mode in single quotes. In an expression such as [A-z], a minus sign represents the current collating sequence. An collating sequence can define an equivalent class for use in a character range. It uses fast deterministic algorithms, sometimes requiring external space. [2]fgrep command to search for a text string for a file.

The fgrep command searches for rows in the matching pattern in the input file specified by the file parameter (the default is standard input). The FGREP command specifically searches for pattern parameters, which are fixed strings. If you specify more than one file Fgrep in the file parameter, the file containing the matching rows is displayed.

The Fgrep command differs from the grep and Egrep commands because it searches for strings rather than the pattern of search matching expressions. The Fgrep command uses a fast compression algorithm. $, *, [, |, (,), and such strings are fgrep by the literal meaning of the command. These characters are not interpreted as regular expressions, but they are interpreted as regular expressions in the grep and Egrep commands.

Because these characters have a specific meaning for the shell, the complete string should be preceded by a single quote (' ...). ’)。

If no file is specified, the FGREP command assumes standard input. In general, each row found is copied to the standard output. If there is more than one input file, the file name is printed before each row found.

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.