The beginner of Linux--regular expression and grep system

Source: Internet
Author: User
Tags expression engine egrep

grep sed awk is called the text-processing Three Musketeers, this article mainly about grep.

Grep:global search Regular Expression and Print out of the line

The global search is performed using regular expressions and the matching rows are displayed;

To gain an in-depth understanding of grep, first recognize and learn the regular expressions, and then we will introduce the regular expressions.


Regular expressions

Regular expression: is the pattern (pattern) written by a class of characters; Metacharacters: does not represent the meaning of the character itself, and is used for the description of the additional functionality.

Regular expression meta-characters:

Basic Regular Expression BRE

GLOBBING------A simplified version of the regular expression: []? *


Character Matching:

. : Matches any single character

[]: matches any single character within the specified range

^: matches any single character outside the specified range

All character sets can be placed in [] to match a single character

[: Lower:]: All lowercase letters

[: Upper:]: All uppercase letters

[: Alpha:]: All letters

[:d Igit:]: All decimal digits

[: Space:]: white space character

[: Alnum:]: Letters or numbers

[:p UNCT:]: All punctuation

[: Xdigit:]: all 16 binary digits

A-Z: all lowercase letters

0-9: all 10 binary digits


Number of occurrences: the number of occurrences of that character before the type of character

*: The preceding character can appear any time (0,1, multiple times)

\?: The characters in front of it are optional (0/1 times)

\+: The preceding character appears at least once (one or more times)

\{num\}: The preceding character must appear m times

\{m,n\}: The preceding character appears at least m times at most n times;

\{,n\}: The preceding character appears at least 0 times at most occurrences n times;

\{m,\}: The characters in front of it appear at least m times, the more the more;


The means by which any character of any length is represented in a regular expression:. *


Positional anchor characters:

Line anchoring:

Beginning of line anchoring: ^

End of line anchor: $

Word Anchor:

The first anchor:\< \b

Word end anchor: \> \b

\b: Anchoring method in old version, not recommended


For the regular expression engine, the word is a continuous string consisting of non-special characters;


Grouping and referencing characters:

\ (pattern\): All characters matched by this PATTERN are treated as an integral whole

In the regular expression engine, there is a series of built-in variables that hold all the character information within the group for the back reference; These variables are: \1, \2 \3 ....

Pattern1\ (pattern2\) pattern3\ (pattern4\ (pattern5\) \)

\1: The character pattrn matches in the first set of parentheses is pattern2

\2: The character that pattrn matches in the second set of parentheses is PATTERN4

\3: The character that pattrn matches in the third set of parentheses is Pattern5


Or:

\|


Note: \| Treat the strings on the left and right side as a whole

Extending the regular expression ERE

parts that are not the same as the basic regular:

+: match the characters in front of it at least 1 times

?: Match its preceding character 1 or 0 times

{M,n}: matches the preceding character at least m times, up to N times
(): Group
A|b:a or B

Continue learning grep After a preliminary understanding of regular expressions

grep [OPTIONS] PATTERN [FILE ...]

PATTERN: Filter condition consists of regular expression meta-characters and text characters without special meanings;

Metacharacters of regular Expressions: the engine of the regular expression is interpreted as a special meaning;

Pcre-perl: Language's regular expression engine

Basic amount Regular Expression: BRE

Extended Regular expression: ERE

GREP supports only basic regular expressions by default

Egrep only extended regular expressions are supported by default

Fgrep the expression engine is not turned on by default


Text characters: Only those characters that have the meaning of the character surface

Common options:

-I,--ingore-case: Ignores the case of text characters

-V,--invert-match: reverse match; The result is a line that pattern does not match successfully

-C,--count: Count, statistics match all lines of pattern

-O,--only-mathing: Turns off greedy mode, showing only what pattern can match

-Q,--Quiet,--silent: Quiet mode, do not output any matching results

--color [=when],--colour[=when]: Displays the contents of the matching pattern in a highlighted form--color=auto

-e: Extended regular expression, grep-e equivalent to Egrep

-f:grep-f equivalent to Fgrep,

-G: Basic regular expression, egrep-g equivalent to grep

-p; using the Pcre (Perl Common Regular Expression) engine

-A num,--after-context=num: Displays the NUM lines that follow the pattern line while displaying it

-B num,--before-context=num: Displays the NUM lines in front of the matching pattern line

-C num,-num,--context=num: Displays the NUM rows before and after the matching pattern line

grep [OPTIONS] [-E PATTERN |-f file] [FILE ...]


By default, only one pattern is allowed behind the grep command, and if you want to use the-e option to write multiple pattern in a single grep search, each-e option can use only one pattern as the parameter;

Writes the required pattern to a file, guaranteeing that there is only one pattern per line, and using the-F file method to implement multiple pattrn as parameters;

Other text-processing commands:

Wc:

Wc[option] ... [FILE]

-L: Show only the number of rows

-W: Displays only the number of words

-C: Show only the number of characters


Cut:remove sections from all line of files

Files that can be trimmed by cut are generally text documents with a certain structure or format;

Cut OPTION ... [FILE] ...

-D,--delimiter=delim: Specifies the delimiter to be relied upon when the pruning operation is implemented, by default a white-space character

-F,--fields=list: Specifies the number of the field according to the defined delimiter

How to use address delimitation:

#: Select a single field to be specified

#,#: Discrete number of individual fields that are specified

#-#: Multiple specified fields in a row

--output-delimiter=string: Specifying the output delimiter


grep Practice Questions:

1) display lines in the/etc/passwd file that do not end with bash;

# grep-v ' bash$ '

2) display the default shell for non-/sbin/nologin users in the/etc/passwd file

# grep-v '/sbin/nologin$ '/etc/passwd

3) Find/etc/grub2.cfg (in the CentOS6 is/etc/grub.conf) file, a non-blank line beginning with a blank character;

# grep--color=auto ' ^[[:space:]]\+[^[:space:]] '/etc/grub2.cfg

  4) displays the current system rootx or bash default shell of the user;

     # grep -E ‘^(root|bash):‘ /etc/passwd | cut -d: -f7

 5)找出/etc/rc.d/init.d/functions文件中某单词后跟一组小括号“()”行;

   # grep -E -o ‘\<[[:alnum:]]+\>\(\)‘ /etc/rc.d/init.d/functions 

6) Find all the file paths in the results of the "ldd/usr/bin/vim" command;

# Ldd/usr/bin/vim | Cut-d '-f3

7) Find out all the lines in the/proc/meminfo file that start with uppercase s or lowercase s, how many ways do you implement the task?

# Cat/proc/meminfo | grep ' ^[ss] '

# Cat/proc/meminfo | grep ' ^[s| S] '

# Cat/proc/meminfo | Grep-i ' ^s '



















The beginner of Linux--regular expression and grep system

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.