The grep for Linux text processing

Source: Internet
Author: User
Tags control characters perl regular expression

the grep for Linux text processing

We often encounter only a few words in a file, such as getting a string of characters from a file or a style, you can use a cat + file one-to-one, so not only inefficient, and a huge amount of characters will let you break. This time need to understand the text Processing three Musketeers, respectively, grep text filtering tool; sed text Editing tool; awk Text Report Generator.

1.grep

all in English: Global search REgular expression and print out of the line (full search of regular expressions and print out the lines). grep is every Linux the release version is preinstalled with the file tool.

2. Role

A powerful file-mode search tool that matches the target text by line and prints out the matching rows based on user-specified "patterns" (filter conditions written by regular expression characters and text characters).

3. Syntax

grep [ options] pattern [file ...]

grep command options:

            

--color=auto keyword highlighting

[Email protected] ~]# cat/etc/passwd |grep--color=auto 514machao2:x:512:514::/root/app:/bin/bashmachao5:x:514:516: :/root/machao1:/bin/bash

The-v keyword displays rows that do not contain a key word,

[Email protected] ~]# cat/etc/passwd |grep-v 1017nologin:x:1013:1018::/home/nologin:/sbin/nologinweiyan:x : 1017:1024::/home/weiyan:/bin/bash

-I keyword ignores the capitalization of keywords

[[email protected] ~]# cat abc.txt aaaabbbbccccdd ee FFAA bbhello world[[email protected] ~]# cat abc.txt |grep ' a ' Aaaabba A bb[[email protected] ~]# cat abc.txt |grep ' a ' Aaaabbaa bb[[email protected] ~]# cat abc.txt |grep-i ' A ' Aaaabbaa Bb

-N Displays the result of adding line numbers before each line

[[email protected] ~]# cat Abc.txt |grep-i-n ' a ' 1:aaaabb4:aa Bb

-C displays only the number of rows found for the result

[[email protected] ~]# cat abc.txt |grep ' [[: Alpha:] '-n1:aaaabb2:bbcccc3:dd ee ff4:aa bb5:hello world[[email protected] ~]# cat Abc.txt |grep ' [[: Alpha:] '-n-c5

-o displays only keywords that match to, and does not display other content from peers

[[email protected] ~]# cat abc.txt |grep ' ee ' dd ee ff[[email protected] ~]# cat abc.txt |grep-o ' ee ' ee

-Q does not output any results, silent

[[email protected] ~]# cat abc.txt |grep-q ' ee ' [[email protected] ~]# echo $?0 (input Instruction No results display, $?) the variable saves the most recent command exit status, resulting in 0 = correct)

-A # Displays the key line and the down n rows

[[email protected] ~]# cat abc.txt |grep-a1 ' ee ' dd ee FFAA Bb

-B # Displays the line of keywords and the n rows up

[[email protected] ~]# cat abc.txt |grep-b1 ' ee ' BBCCCCDD ee ff

-C # displays key lines and n rows up and down n rows

[[email protected] ~]# cat abc.txt |grep-c1 ' ee ' bbccccdd ee ffaa Bb

-e keyword 1-e keyword 2 ... Relationships between multiple keywords Yes or

[email protected] ~]# cat abc.txt |grep-e aa-e ff-e BBAAAABBBBCCCCDD ee ff

-w keyword matches entire word

[[email protected] ~]# cat abc.txt aaaabbbbccccdd ee ffaa bbaa aa bbhello world[[email protected] ~]# cat abc.txt |grep AA Aaaabbaa AA Bb[[email protected] ~]# cat abc.txt |grep-w AAAA AA BB

-e equals egrep using extended regular expressions

-F equals fgrep does not use regular expressions

This is just the beginning of the grep command, and you may have noticed that it is simply too useful to implement a variety of requirements. In addition to this one-line command that we run, grep can also be written as a C automatic shell script to execute.

4. Regular Expressions

(1) a pattern written by a class of special characters and text characters, where some characters (metacharacters) do not represent literal meanings of characters, but are functions of control or wildcard. Program support: Grep,sed,awk,vim, less,nginx,varnish, etc.

(2) divided into two categories: basic regular Expressions (basicRegular expression also known as basic RegEx abbreviation Bres)

Extended Regular Expressions (Extended Regular expression also called Extended RegEx abbreviation Eres)

perl Regular Expressions (Perl Regular expression called Perl RegEx abbreviation pres)

(3) Composition: General characters: characters with no special meaning

Special characters (meta characters): metacharacters, which have special meanings in regular expressions

(4) Metacharacters and their behavior in the context of regular expressions

\  marks the next character as a special character, or a literal character, or a back reference, or an octal escape character ^  The starting position of the matching input string. If you set the multiline  property of the  RegExp  object,^  also matches the location of   ' \ n '   or   ' \ R '   after *  Matches the preceding subexpression 0 or more times +   matches the preceding subexpression one or more times. +  equivalent to  {1,}?   matches the preceding subexpression 0 or one time.?  equivalent to  {0,1}{n} n  is a nonnegative integer that matches the determined n  times {n,}  n  is a nonnegative integer that matches at least n  times {n,m} m  and  n  are non-negative integers, where n <= m. Minimum match  n  times and up to  m  times. There cannot be a space between a comma and two numbers?   when the character immediately follows any other restriction   (*, +, ?, {n}, {n,}, {n,m})   , the matching pattern is non-greedy.     non-greedy mode matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible .   matches any single character except   "\ n"   $    matches the preceding regular expression, at the end of a string or line. The BRE has a special meaning only at the end of the regular expression, and Ere has a special meaning in any position []  matches any one of the characters within the square brackets, where the hyphen (-) is used to refer to the range of contiguous characters; ^ symbol Bitter appears in the first position of the square brackets, which matches any character that is not in the list \ <  or  \b  header anchor for left  \>  or  \b  ending anchoring of the word pattern; right side for Word mode  \<PATTERN\>   Match Whole word 

               (5) Character set

[::alnum] :  Digital Characters                  [:d igit:] :  Digital characters                  [:p unct:] :  Punctuation characters     [:alpha:] :  alphabetic characters                  [:graph:] :   non-whitespace characters               [:space:]  :  space characters     [:blank:] :  spaces with positioning characters            [:lower:] :  Lowercase alphabetic characters              [:upper:] :  uppercase characters     [:cntrl:] :  control characters                  [:p rint:] :  characters that can be displayed             [:xdigit:] : 16 binary numbers 

(6) Common regular Expressions

^$ Empty line ^[[:space:]]*$ blank line/< (. *) >.*<\/\1>|< (. *) \/>/matches regular expressions for HTML tags/(\d+) \. (\d+) \. (\d+) \. (\d+)/g//match the regular expression "\< ([0-9]|[) of the IP address 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "matches the regular expression \w+ of an IP address ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * Match the regular expression of the email address http://(/[\w-]+\.)    +[\w-]+ (/[\w-./?%&=]*)? Regular expressions that match URL URLs


The grep for Linux text processing

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.