You need to use regular expressions when using grep, awk, and SED commands.
For example, I use grep to find out if there are errors in the code compilation results. Or if there is an error in my code.
Here is the basic application of regular expressions:
Matches the beginning and end of the line.
Match the data set.
Matches only letters and numbers.
Matches a set of strings within a certain range.
| ^ |
Match only the beginning of the line |
| $ |
Match end of Line only |
| * |
A single character immediately following *, matching 0 or more characters |
| [] |
Matches the character in [], which can be a single character or a sequence of characters. can be used-to denote [] the range of character sequences, as in [1-5] instead of [12345] |
| \ |
A special meaning used to mask a meta-character. Because sometimes some metacharacters have special meanings in the shell. \ can make it lose its proper meaning. |
| . |
Match any single character |
| Pattern\{n\} |
Used to match the number of occurrences of the preceding pattern. n is the number of times |
| Pattern\{n,\} |
Same as above, but with a minimum number of n |
| Patter\{n,m\} |
Same as above, but pattern occurs between N and M |
match single character with a period
Period "." can match any single character. For example, if you want to match a string, start with be g, clip a
Any character, then it can be represented as be G. N, "." You can match a string header or any character in the middle.
In the Ls-l command, you can match certain permissions:
.. . X. . X. . X
This format matches the execution rights of the user itself, user groups, and other group members.
~$ ls-l |grep ... x.. X.. X
DRWXRWXRWX 1 h00209633 Domain U 1 month 08:38 bin
DRWXRWXRWX 1 h00209633 Domain U 2 month 7 08:06 Emacs
DRWXRWXRWX 1 h00209633 Domain U 1 month 08:38 etc
DRWXRWXRWX 1 h00209633 Domain U 1 month 08:38 info
DRWXRWXRWX 1 h00209633 Domain U 1 month 08:38 Leim
DRWXRWXRWX 1 h00209633 Domain U 2 month 7 09:05 Lisp
DRWXRWXRWX 1 h00209633 Domain U 2 month 7 08:07 site-lisp
~$
-
at the beginning of the line with a ^ match string or character sequence
^ Only the start of a line is allowed to match characters or words. For example, use the Ls-l command and match the directory. It is possible to
~$ ls-l |grep ^d
drwxrwxrwx 1 h00209633 Domain u 01 months 08:38 bin
drwxrwxrwx 1 h00209633 Domain u 2 month 7 08:06 emacs
DRWXRWXRWX 1 h00209633 domain u 1 month 08:38 etc
drwxrwxrwx 1 h00209633 Domain u 1 months 08:38 info
DRWXRWXRWX 1 h00209633 domain u 2 month 7 09:05 lisp
drwxrwxrwx 1 h00209633 domain u 2 months 7 08:07 site-lisp
Match a string or character at the end of a row
It can be said that in contrast to ^, it matches a string or character at the end of a line, and the $ symbol is placed after the matching word. Assume that you want to match the
The word lisp ends with all lines that operate as:
~$ ls |grep sp$
Lisp
Site-lisp
~$
Use * to match single characters or their repeating sequences in a string
Use this special character to match any character or string that repeats multiple expressions. For example:
Find E*m (string ending with e starting with M)
~$ ls | grep e*m
Emacs
Leim
~$
use \ To mask the meaning of a special character
Sometimes you need to find some characters or strings, and they contain a character that the system specifies as a special character.what
Is it a special character? In general, the following characters can be considered special characters:
$ . ' "* [] ^ | { } \ + ?
Example: Find a file with an. El suffix
~/lisp$ ls | grep \.el$
Abbrev.el
Abbrevlist.el
Add-log.el
Align.el
Allout.el
Ansi-color.el
Apropos.el
Arc-mode.el
Array.el
Autoarg.el
Autoinsert.el
Autorevert.el
Avoid.el
Battery.el
Bindings.el
Bookmark.el
Bs.el
Buff-menu.el
Button.el
Calculator.el
Case-table.el
use [] to match a range or collection
Using [] matches a specific string or set of strings, you can separate the different strings to be matched in parentheses with commas, but
This is not mandatory (some systems promote the use of commas in complex expressions), which can increase the readable
Of
Use "-" to denote a range of strings, indicating that the string range starts with the character "-" to the left of "-" and to the "-" right word
End of the symbol.
If you are familiar with a string match operation, you should use the [] pattern frequently.
Assuming that you want to match any number, you can use:
[0123456789]
However, you can simplify the operation by using the "-" symbol:
[0-9]
or any lowercase letter
[A-z]
To match any letter, use:
[A-za-z]
Indicates the range of letters from a-Z, A-Z.
To match any letter or number, the pattern is as follows:
[A-za-z0-9]
Note the use of the notation, when used directly in the first parenthesis, means to negate or not match the bracketed contents.
[^a-za-z]
Matches any non-alphabetic character, and
[^0-9]
Matches any non-numeric character.
In the last example, you should be able to guess besides using ^, there are some ways to search for any special character.
Number of occurrences of matching pattern results using \{\}
Use * to match all matching results any time, but if you specify the number of times, you should use \{\}, this mode has three kinds of
form, namely:
pattern\{n\} match pattern appears n times.
Pattern\{n,\} The matching pattern appears at least n times.
PATTERN\{N,M} The matching pattern occurs between N and M times, N, M is 0-2 5 5 in any integer.
For example, the format is as follows: The first 4 characters are numbers, the next is XX, the last 4 are numbers,
The operation is as follows:
[0-9]\{4\}xx[0-9]\{4\}
The specific meanings are as follows:
1) The matching number appears 4 times.
2) followed by code XX.
3) Finally, the number appears 4 times.
Examples of regular expressions that are used frequently
^ Beginning of the line
$ End of line
^ [t h e] start with t h e line
[s] I g n a [l l] Match word s I G n A l, S i g n A l, S i g n A l, S i g n a l
[Ss]igna[ll] ". Ditto, but add a period
[M a y m a Y] a line containing M a y uppercase or lowercase letters
^ U S e R $ only contains the lines of U S e r
[tty]$ line ending with T T y
" . Line with a period
^ D. . X. . X. . x directories that have executable permissions on users, user groups, and other user group members
^ [^ l] Exclude directory list of associated directories
^[^d] ls–l | grep ^[^d] Show only files that are not folders
[. * 0] 0 before or after adding any character
[0 0 0 *] 0 0 0 or more
[II] Uppercase or lowercase i
[I i] [n n] Uppercase or lowercase i or n
[^ $] blank line
[^. * $] matches any string in a row
^ . . . . . . $ includes 6-character lines
[A-za-z] any single character
[A-z] [A-z] * At least one lowercase letter
[^ 0-9 "$] non-digital or US dollar logo
[^ 0-0 A-z] non-numeric or letter
[1 2 3] 1 to 3 a number
[D d] e v i c e Word d e v i c e or d e v i c E
D E. . C e The first two letters are D e, followed by two arbitrary characters, and finally C E
^ Q start line with ^ q
^ . $ only one character line
^". [0-9] [0-9] lines starting with a period and two digits
' "D e v i c e" ' word d e v i c E
D e [v v] I c e ". Word d e v i c e or d e v i c E
[0-9] "{2"}-[0-9] "{2"}-[0-9] "{4"} pair date format d d-m m-y y y y
[0-9] "{3"} ". [0-9] "{3"} ". [0-9] "{3"} ". [0-9] "{3"} I p address format
[^. * $] matches any row
[a-za-z]* matches all words
Common G r e p options
-C outputs only the count of matching rows.
-I is case-insensitive (only for single-character).
-H does not display a file name when querying multiple files.
-L Only output file names that contain matching characters when querying multiple files.
-N Displays matching lines and line numbers.
-S does not display error messages that do not exist or have no matching text.
-V Displays all lines that do not contain matching text.
This article is from the "Shisen" blog, make sure to keep this source http://shisen.blog.51cto.com/11862607/1852700
Using regular expressions in Linux commands