1. Text Processing tools (grep, sed, awk)
grep: Text Filter tool (mode: pattern);
Sed:stream Editor, stream editors, text editors;
The implementation on Awk:linux is Gwak, Text Report Generator (formatted text);
Regular expressions are used in all three of these tools.
2. Regular Expressions: (Regual expression,regexp): A pattern written by a class of special characters and text characters, some of which do not represent their literal meaning, but are used to represent a function of control or wildcard.
Regular expression classification: The basic regular Expression Bre, extended regular expression ere. (The difference between the two: meta-characters are different)
Regular expression meta-characters:
grep: Searches each line of the file thoroughly and matches the pattern (filter condition) to output matching rows .
Pattern: The filter condition written by metacharacters and text characters of regular expressions.
Regular expression engine:
grep usage:
grep [OPTIONS] PATTERN [FILE ...]
grep [OPTIONS] [-E PATTERN |-f file] [FILE ...]
OPTIONS:
--color=auto: Color highlighting of matched text;
-i:ignorecase; ignore character case;
-O: Displays only the matching string itself, and the default display is the matching line;
-v:--invert-match; Displays rows that cannot be matched to a pattern;
-E: Supports the use of extended regular expressions;
-q:--quit,--silent; do not output anything
-A #: show the after # lines that match to
-B #: Displays the first # lines matched to before
-C #: Displays the front and back # lines that match to; context
Basic Regular Expression metacharacters: (classification: Character matching: Number of matches, location anchoring, grouping, and referencing)
Character Matching:
.: Matches any single character (point number)
[]: matches any single character within the specified range;
[^]: matches any single character outside the specified range;
[[:d Igit:]]; [[: Lower:]]; [[: Upper:]]; [[: Alpha:]]; [[: Alnum:]]; [[:p UNCT:]]; [[: Space:]]
[[email protected] ~]# grep r[[:alpha:]][[:alpha:]]t/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
[Email protected] ~]#
Number of matches: used to limit the number of occurrences of the preceding character, after the character to specify the number of occurrences.
*: Match its preceding characters any time: 0, 1, multiple times;
. *: Matches any character of any length;
\?: matches the preceding character 0 or 1 times, that is, the preceding character is optional;
\+: Matches the preceding character 1 or more times, i.e. the preceding character must appear at least 1 times;
\{m\}: Matches the preceding character m times;
\{m,n\}: Matches its preceding character at least m times, up to n times;
\{0,n\}: Up to n times;
\{m,\}: at least m times;
[[email protected] ~]# grep ro*t/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Abrt:x:173:173::/etc/abrt:/sbin/nologin
Rtkit:x:172:172:realtimekit:/proc:/sbin/nologin
Pkiuser:x:17:17:certificate System:/usr/share/pki:/sbin/nologin
[Email protected] ~]#
Location anchoring:
^: Anchor at the beginning of the line: for the leftmost mode;
$: End of line anchoring: for the right-most side of the pattern;
^pattern$: Used for PATTERN to match whole line;
^$: blank line;
^[[:space:]]*$: Blank lines or lines that contain white space characters;
Word: A continuous character (string) consisting of a non-special character is called a word;
\< or \b: The first anchor of the word, the left side of the word pattern;
\> or \b: The ending anchor for the right side of the word pattern;
\<pattern\>: matches complete words;
[[email protected] ~]# grep "\<root"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep "\broot"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep "root\>"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
[Email protected] ~]#
[[email protected] ~]# grep "^root"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# grep "root$"/etc/passwd
[Email protected] ~]#
Grouping and referencing:
Such as:
[[email protected] ~]# grep uuid/etc/fstab
Uuid=47c2d0d2-2c32-4a36-89e7-420739594a95/boot XFS Defaults 0 0
[Email protected] ~]#
Regular Expressions of Linux