Linux basic command grep egrep fgrep usage and Regular Expression

Source: Internet
Author: User

Linux basic command grep egrep fgrep usage and regular expression 1. grep, egrep, and fgrep commands this article mainly introduces the basic parameters and usage formats of grep egrep fgrep commands and regular expressions in linux, method. (Note: fg stands for an example in this article.) 1.1. basic definition: www.2cto.com grep (global search regular RE) and print out the line, fully search for regular expressions and print the rows) is a powerful text search tool. It can only use basic regular expressions to search text and print matching rows. Grep is a common and frequently-used command. Its main function is to compare string data and print the strings that meet user requirements, when grep looks for a string in the data, it filters the data in the unit of "whole row. The egrep command is equivalent to grep-E. With this command, you can use an extended regular expression to search for text and print the strings that meet your needs. The fgrep command is equivalent to grep-F. It uses a fixed string to search for text, but does not support regular expression reference. Therefore, this command can be executed at the fastest speed. 1.2 Basic command usage grep [option] 'search string' filename grep common options:-a: search data in binary files as text files-c: calculate the number of times the 'search string' is found-I: case-insensitive-v: reverse lookup, that is, the line without the 'search string' content is displayed-o: show only strings that are matched by the pattern-n: output line number -- color (color): color Display-A: show the n rows that match the character-B: show the first n rows that match the character-C: show the first n rows that match the character-2. Regular Expression 2.1. basic definition: regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern. In short, a regular expression is a string processing method. It processes strings in the unit of action. With the help of some special symbols, you can easily search for/replace a specific string. Regular Expressions are classified into two types: Basic Regular Expressions and extended regular expressions. 2.2 Regular Expressions 2.2.1. Basic Regular Expressions: (1) metacharacters :.: match any single character fg: Find the row grep 'student with a character after student. '/etc/passwd (single quotation marks and double quotation marks can be used in the mode. If you want to replace variables in the mode, double quotation marks must be used.) []: match any single character within the specified range, [abc], [a-z], [0-9], [a-zA-Z] fg: search for rows with numbers grep '[0-9]'/etc/passwd [^]: match any single character out of the specified range fg: Find rows without lowercase letters. Grep '[^ a-z]'/etc/inittab [: space:]: indicates a blank character [: punct:]: indicates a set of all Punctuation Marks [: lower:]: indicates all lowercase letters [: upper:]: indicates all uppercase letters [: alpha:]: indicates the upper and lower case letters [: digit:]: indicates the number of digits [: alnum:]: it indicates numbers and uppercase/lowercase letters ----- use the format [[: alnum:] and so on (2) times to match: *: match any character before it fg: find the row grep 'root * '/etc/passwd that appears 0 or more times as the root user. *: any character fg: Find the row grep 'root containing the root. * '/etc/passwd \?: Match the first character once or 0 times \ {m, n \}: match the first character at least m, up to n times) (3) character anchoring: ^: pin the beginning of the line. Any content after this character must appear at the beginning of the line fg: Find the line whose first line starts with # grep '^ #'/etc/inittab $: pin the end of a row. Any content before this character must appear at the end of the row fg: Find the row whose first line ends with root grep 'root $ '/etc/inittab ^ $: the blank line can be anched to count the blank line \ <or \ B: the beginning of the anchor. Any character after the blank line must be used as the word header to show fg: find the line grep '\ <root'/etc/man that contains no characters before root. config \> or \ B: pin the end Of the word. Any character before it must be used as the end of the word to show fg: \ <root \> find the root word grep "\ <root \>" = grep "\ broot \ B" 2.2.2. Extended regular expression: the extended regular expression is only in the basic regular expression table. A slight modification was made to the parameter. The modification is as follows: in the extended regular expression, \ (\) is written as (), \ {\} is written as {}, and + is added: number of times match, match the character before it at least once, no upper limit, |: Or (two take one), the rest are the same, basic regular expression, use (){}.? | Escape is required. \ is not required in the extended regular expression. The detailed information is as follows: (1) the character matching command and usage are the same as the basic regular expression usage, I will not repeat it here. (2) times matching: *: Any times that match the first character?: Match the first character 0 or 1 +: match the first character at least 1 This fg: at least one blank character: '[[: space:] +' {m, n}: match the previous characters m to n times (3). the usage of the character anchoring is the same as that of the basic regular expression. (4) Special Character: |: represents or. Fg: grep-E 'C | cat' file: searches for files containing c or cat \.: \ represents an escape character, which represents a symbol. 3. The grep command uses a small instance (1) to display the/etc/inittab that starts with # and is followed by one or more blank characters, then it followed any non-blank line grep' # [[: space:] * [^ [: space:] '/etc/inittab (2) the output is not a numeric switch row grep '^ [^ 0-9]'/etc/passwd (3) the first output line is 1 or 2 grep '^ \ (1 \ | 2 \)'/etc/inittab or grep-E '^ (1 | 2) '/etc/inittab (4) search for any indirect character in rc before/rc grep '. * \ (rc \). * \/\ 1. * '/etc/inittab (5) retrieves the IP address ifconfig on the current computer | grep-A 1 "^ eth0" | grep "\ <[0-9.] \ {1 ,\}| cut-d:-f2 (6) query the information of the user account whose name is student (must appear at the beginning of the row) on the current system, the file is/etc/passwd grep "^ student"/etc/passwd.

Related Article

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.