Regular expressions
is a pattern written by a class of characters, many of which do not represent their literal meaning, but rather express control or wildcard functions; a regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.
Metacharacters: does not represent its literal meaning, but is used for additional functional descriptions, below is a list of commonly used metacharacters
Symbol |
Meaning |
|
means match any single character, example A., match AB, AC, AD, ax |
* |
Span style= "font-family: ' The song Body '; > can appear any time, example a*b, matching AAB, B, ab |
\? |
means The character in front of it is optional, example A\?b, Match AB, B, Xb |
\{m\} |
Span style= "font-family: ' The song Body '; > denotes M-Times , example a\{2\}b, matching AAB , Aaaab , Aaaaaab BBB |
\{m,n\} |
Span style= "font-family: ' The song Body '; > indicates that the character before it appears at least m times, up to n times, example a\{2,5\}b, matching AAB, Aaab, |
\{m,\} |
Span style= "font-family: ' The song Body '; > indicates that the characters in front of it appear at least m times, example a\{2,\}b, matching AAB, aaaaaaaaaaaab bbbb |
\{0,n\} |
Span style= "font-family: ' The song Body '; > indicates that the characters in front of it appear up to n times, example a\{0,3\}b, Match B, AAB, Aaaaab bbbb |
.* |
Span style= "font-family: ' The song Body '; > denotes , greedy mode, as many as possible to match |
^ |
Span style= "font-family: ' The song Body '; > Indicates the beginning of a line anchor, example ^a, representing a row beginning with a |
$ |
Span style= "font-family: ' The song Body '; > represents the end of line anchoring, example $ A, representing the line ending with a |
^$ |
Span style= "font-family: ' The song Body '; > indicates a blank line |
\< |
Span style= "font-family: ' The song Body '; > Indicates the first anchor of the word, appearing on the left word of the word: a string of consecutive characters that do not contain special characters |
\> |
Span style= "font-family: ' The song Body '; > represents the ending anchor, appearing on the right side of the word |
\# |
Span style= "font-family: ' The song Body '; It refers to the content that the nth parenthesis matches to, not the schema itself, example \ (ab\). *\1, matching ab sdfdf ab |
+ |
Indicates that the character before it appears at least once |
Linux grep and Egrep
grep (Global search Regular expression (RE) and print out of the line, full search of regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching lines.
Extended Regular expression: Egrep or Grep-e
Basic Regular Expressions: grep
Common parameters:
-V: Reverse Selection
-O: Displays only the matched string, not the line where the string is located
-i:ignore-case, ignoring character case
-E: Supports the use of extended regular expressions
The biggest difference between Egrep and grep: Egrep is the expanded version of grep, which improves the operation of many traditional grep that cannot or is inconvenient. Say:
-Not supported under grep? With + These two kinds of modifier, but Egrep is available.
-grep does not support a|b or (ABC|XYZ) such "or one" pairs, but egrep can.
-grep needs \{and \} When processing {n,m}, but egrep is not required.
Examples
1. Display the lines in the/proc/meminfo file that begin with uppercase or lowercase s;
# grep-i ' ^s '/proc/meminfo
# grep ' ^[ss] '/proc/meminfo
# grep-e ' ^ (s|s) '/proc/meminfo
2. Display the default shell of the/etc/passwd file as a non-/sbin/nologin user;
# grep-v "/sbin/nologin$"/etc/passwd | Cut-d:-f1
3. Display the user whose default shell is/bin/bash in/etc/passwd file;
Further: Display only the user whose ID number is the highest in the above results;
# grep "/bin/bash$"/etc/passwd | SORT-T:-k3-n | Tail-1 | Cut-d:-f1
4. Find out one or two digits in the/etc/passwd file;
# grep "\<[0-9][0-9]\?\>"/etc/passwd
# grep "\<[0-9]\{1,2\}\>"/etc/passwd
5. Display the line beginning with at least one blank character in/boot/grub/grub.conf;
# grep "^[[:space:]]\{1,\}"/boot/grub/grub.conf
6, display/etc/rc.d/rc.sysinit file, start with #, followed by at least one white space character, and then have at least one non-whitespace character line;
# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}"/etc/rc.d/rc.sysinit
7. Find the line ending with ' LISTEN ' in the result of netstat-tan command execution;
# Netstat-tan | grep "listen[[:space:]]*$"
8, add user bash, Testbash, basher, Nologin (shell for/sbin/nologin), and find the current system on its user name and the default shell of the same user;
# grep "^\ ([[: Alnum:]]\{1,\}\):. *\1$"/etc/passwd
10. Display the default shell of root, fedora or User1 user on the current system;
# grep-e "^ (root|fedora|user1):"/etc/passwd | Cut-d:-f7
11, find a word in the/etc/rc.d/init.d/functions file followed by a set of parentheses "()" line;
# Grep-o-E "\<[[:alnum:]]+\>\ (\)"/etc/rc.d/init.d/functions
12. Use the echo command to output a path, and then use grep to remove its base name;
echo "/etc/sysconfig/" | Grep-o-E "[[: alnum:]]+/?]
# echo "/etc/sysconfig/" | Grep-o-E "[^/]+/?$" | Cut-d/-F1
13. Find the number between 1-255 in the result of ifconfig command;
# Ifconfig | Grep-o-E "\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "
This article is from the "Fish" blog, please be sure to keep this source http://kyfish.blog.51cto.com/1570421/1437655