Next day Linux filtering, regular expressions

Source: Internet
Author: User

1. Filter text content----grep

2. Use
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. grep full name is global Regular Expr

Ession Print, which represents the global regular expression version, whose usage rights are all users.

2. Format
grep [Options]

3. Main parameters
[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.

Option Description:

        • –color=auto: Color matching to text, highlighting
        • -E: represents a regular expression that supports the use of extensions, equivalent to Egrep
        • -Q: Silent mode, does not output any information to standard output
        • -A (after): Displays the line that matches to and the line following it, giving a number
          • grep -A 1 root /etc/passwd
        • -B (before): Displays the line that matches to and the line preceding it, giving a number
          • grep -B 1 root /etc/passwd
        • -C (center): Displays the line that matches to and the line before and after it, giving a number
          • grep -C 1 root /etc/passwd


Regular expression Regex

Match a single character's meta-character

1). Any single character

[[email protected] ~]# grep "R.. T "/etc/passwd

2) [a1b] square brackets any one character

[[Email protected] ~]# "grep r[abc]t"/etc/passwd

-Continuous range of characters

[A-z]     [A-z]    [A-za-z] [0-9] [a-za-z0-9]

^ Take the reverse

[^a-z]

[[email protected] ~]# grep "A[^a-z]t"/tmp/1.txt


3) Special Character set

[[:d Igit:]] any single number
[[: Alpha:]] any single letter
[[: Upper:]] any single uppercase letter
[[: Lower:]] any single lowercase letter
[[: Alnum:]] Any single digit, letter
[[: Space:]] Any single white space character
[[:p UNCT:]] any single punctuation

[[email protected] ~]# grep "A[[:upper:]]t"/tmp/1.txt



Where the match character appears

^string starts with string

[[email protected] ~]# grep "^root"/etc/passwd

[[email protected] ~]# grep "^[a-z]"/tmp/1.txt

[[email protected] ~]# grep "^[^a-z]"/tmp/1.txt


String$ ends with string

[[email protected] ~]# grep "bash$"/etc/passwd


^$ Empty Line

[[email protected] ~]# grep "^$"/etc/fstab | Wc-l

| Pipeline
function: The result of the previous output as the condition of the following command

Number of occurrences of matching characters

* The previous character appears any time ab*

[[email protected] ~]# grep "ab*"/tmp/2.txt

\? Previous character appears 0 or 1 times optional

[Email protected] ~]# grep "ab\?"/tmp/2.txt

\+ the previous character appears 1 or more times

[[email protected] ~]# grep "ab\+"/tmp/2.txt


\{4\} The previous character appears exactly 4 times

[[email protected] ~]# grep "ab\{3\}"/tmp/2.txt

{2,4}, {2,}

[[email protected] ~]# grep "ab\{2,4\}"/tmp/2.txt

[[email protected] ~]# grep "ab\{2,\}"/tmp/2.txt


Group \ (ab\)

[[email protected] ~]# grep "\ (ab\) \{2,\}"/usr/share/dict/words


Option options:

-I ignores case

[Email protected] ~]# grep-i "^r"/tmp/1.txt


-O Show only pattern-compliant content

[Email protected] ~]# Grep-o "R.. T "/etc/passwd


-e filters content based on multiple criteria at the same time

[Email protected] ~]# grep-e "^#"-E "^$"/etc/fstab


-V Reverse Filter

[Email protected] ~]# grep-v "^#"/etc/fstab

[Email protected] ~]# grep-v-E "^#"-E "^$"/etc/fstab


-E supports extended regular expressions

[[email protected] ~]# grep-e "(AB) {2,}"/usr/share/dict/words

[Email protected] ~]# grep-e "Bin|sbin"/etc/passwd


-a n Displays the contents of the following n rows that match the criteria

[Email protected] ~]# ifconfig eth0 | GREP-A 1 "broadcast" >>> view IP address information for eth0 NIC
inet 192.168.122.105 netmask 255.255.255.0 broadcast 192.168.122.255
Inet6 fe80::5054:ff:fe71:3b1c Prefixlen ScopeID 0x20<link>


-B N Displays the first n rows that match the criteria

[[Email protected] ~]# IP addr show eth0 | Grep-b 1 "Global" >>> view the IP address and MAC address of the network card
Link/ether 52:54:00:71:3b:1c BRD FF:FF:FF:FF:FF:FF
inet 192.168.122.105/24 BRD 192.168.122.255 scope Global Dynamic eth0

Wildcard characters

      • *: matches any character of any length, can have no
      • ? : matches any single character and must have a
      • []: matches a single character within a specified range
      • [^]: matches any character outside a specified range
        • [^[0-9]] or [^0-9]: represents any character outside of a matching number
        • [^[:upper:]]: represents any character outside of the uppercase letter
        • PA[0-9]: The PA is followed by a number
        • [A-z], [a-z] [0-9] [a-z0-9] [ABDXY]
        • PA[0-9]: The PA is followed by a number
        • PA[0-9][0-9]: Two digits after the PA
        • [[: Upper:]]: denotes all uppercase letters
        • [[: Lower:]]: denotes all lowercase letters
        • [[: Alpha:]]: denotes all letters
        • [[:d Igit:]]: denotes all numbers
        • [[: Alnum:]]: denotes all letters and numbers
        • [[: Space:]]: denotes all whitespace characters
        • [[:p UNCT:]]: denotes all punctuation

        • Adhere to the habit, is not a summary of their own things, just want to have a note.

Next day Linux filtering, 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.