2017-12-16linux basic Knowledge (EGREP) extended Regular expression

Source: Internet
Author: User
Tags diff echo command file diff expression engine egrep


Recalling the previous chapter, in the previous chapter we described the Text Processing tool grep command, which is performed by the user according to the specified "pattern" for row matching, the default is greedy mode, the matching results are printed to standard output, and the regular expression is written by a group of special characters, Some of these characters do not represent the original literal meaning, but for the function of control or wildcard, and the regular expression of two categories, one is the basic regular expression, the other is extended regular expression, the difference is that their meta-characters are different, and the meta-character is used to match and control the function, So in this chapter we tell about Egrep and extending regular expression commands.

First, Egrep command

In addition to the grep commands we talked about, and the egrep and Fgrep commands, grep now knows that it supports basic regular expressions, but uses-Eoption to support extended regular expressions, the EGREP command features support for extending regular expressions, but using the-Goption is able to support basic regular expressions, then Fgrep does not support the basic regular expressions, nor does it support extended regular expressions, but with these two options you can support regular expressions, as well as the above two whether grep and egrep are used-FThe options are the same as fgrep. Basic regular expressions and extended regular expressions are not supported. In other words, these three commands can support the characteristics of any two commands by adjusting their functionality with some options.
Then Egrep, which is a regular expression that supports scaling, implements the functionality similar to grep text filtering, and can also be supported with GREP-E, and its command format is the same as the grep command:

egrep [Options] PATTERN [FILE ...]

Then the supported options are the same as grep:

-I,-O,-V,-A,-B,-c-g: supports basic regular expressions;

Then let's talk about the meta-character of the extended regular expression, which has a lot of the same meta-characters as the metacharacters and basic regular expressions, except that there are no escape characters.

    character matching:       .: any single character;       [] : Any single character within the specified range;       [^]: Specify any single character;    the number of occurrences outside the range:       *: Any time, 0, 1 or multiple times,       ?:0 or 1 times, the preceding characters are optional.       +: Its preceding character at least once;       {m}: Its preceding character M-Times;       {m,n}: At least m times, up to N times;       {0,n}        {m}            Location Anchor:       ^: Anchor at the beginning of the line;       $: End Anchor;       \<, \b: The first anchor of the word;       \>, \b: the final anchor;         grouping and referencing:       (): grouping: pattern matching in parentheses to characters is recorded hermetical the internal variables of the expression engine;        back reference: \1, \2, .  (cannot be referenced in bash)     or:       a|b:a or b         c|cat:c or cat;         (C|c) At:cat or cat;

Well, the above is the meta-character that extends the regular expression, so now practice the following exercises:
1. Find all the lines in the/proc/meminfo, all in uppercase or lowercase s, and at least three implementations;

# grep ^[ss]/proc/meminfo # grep-e "^ (s| S) "/proc/meminfo # grep-i" ^s "/proc/meminfo

2. Displays information about root, CentOS, or User1 on the current system:

# grep-e "^ (root|centos|user1) \>"/etc/passwd

3, find the/etc/rc.d/init.d/functions file in a word followed by a parenthesis line;

# grep-eo "^[_[:alpha:]]*\ (\) [[: space:]]*"/etc/rc.d/init.d/functions

4, using the echo command output an absolute path, using Egrep to remove the base name;

# echo "/etc/sysconfig" | Grep-eo "[^/]+?$"

5. Find the value between 1-255 in the result of ifconfig command;

# Ifconfig | Grep-eo "[1-9]| [1-9] [0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4] "

Then Fgrep does not support regular expression metacharacters, and when there is no need to use metacharacters to write patterns, it is better to use the fgrep effect, and to filter logs in a production environment.

Second, text viewing and processing tools

At this time we mainly talk about the following commands:

WC, cut, Sort, Uniq, diff, Patch

The WC command is a word-count command that mainly counts the number of rows, words, and bytes in the command format:

WC [OPTIONS] ... [FILE] ...

The options for this command are:

-l:lines, counts the number of rows;-w:words, counts the number of words;-c:bytes, Statistics bytes;

The cut command extracts fragments from each line of the file and displays the extracted fragments to the standard output in each row of the file. Its command format is:

Cut OPTIONS [FILE] ...

The options for this command are:

-D CHAR: Delimited by the specified character, or, if not, by default, blank as delimiter,-F fileds: Selected field, #: Multiple fields specified, #-#: Multiple fields in succession, #,#: discrete multiple fields;

The sort command is a sorted command, which is a line of sorted text files with the following command format:

Sort [OPTIONS] ... [FILE] ...

The options for this command are:

-N: Sort based on numeric size instead of characters;-T CHAR: Specifies delimiter;-k#: The character used for sorting comparisons;-R: reverse order;-F: Ignores character case;-U: Duplicate rows are kept only one copy; repeating rows: continuous and identical;

Example:

# sort-t:-k3-n-r/etc/passwd

The Uniq command is a report or remove duplicate rows, similar to the-u option in the sort command, and its command format is:

Uniq [OPTIONS] ... [INPUT [OUTPUT]]

The command options are:

-C: Displays the number of repetitions for each row;-u: Displays only rows that have not been repeated;-D: Displays only repeated rows;

Then the diff command is used to match the file row by line, you can merge it, to patch the old file, its command format is:

diff [OPTIONS] ... FILE diff/path/to/oldfile/path/to/newfile >/path/to/patch_file

A patch is a command that patches a file with the following command format:

Patch [OPTIONS]-i/path/to/path_file/path/to/oldfile Patch/path/to/olddile </path/to/patch_file


2017-12-16linux basic Knowledge (EGREP) extended Regular expression

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.