Wildcards and Regular Notation (regular expressions) in the shell

Wildcards and special symbols
* in the shell represent "zero or infinite" arbitrary characters 
? Represents "must have one" arbitrary characters 
[ ] also represents "must have one in brackets" characters (non-arbitrary characters). For example, [abcd] means "there must be one character, which may be any of the four a, b, c, d"
[-] If there is a minus sign in square brackets, it means "all characters in the encoding sequence". For example [0-9] represents all numbers between 0 and 9, because the language encoding of numbers is continuous!
[^] If the first character in the square brackets is an exponential symbol (^), it means "reverse selection", for example [^abc] means there must be one character, except for other characters other than a, b, c meaning to accept.
# Annotation symbol: This is most often used in script, which is a description! The following data are not executed. Comment
\ Escape symbol: restore "special characters or wildcards" to general characters
| Pipeline: the definition of dividing two pipeline commands
; continuous command issuing separator: the definition of continuous commands
~ user home directory
$ access variables Leading character: that is, the variable that needs to be added before the variable to replace the value. The return value of the last command, 0 is successful execution
& job control (job control): send the command to work in the background 
! "Not" in the sense of logical operation
/ directory symbol: the symbol of path division
< << data redirection, input guide
> >> Data redirection, output orientation, respectively replacing and appending
'' Single quotes do not have variable substitution function
"" Has variable substitution function``
The middle of the two " `" is an instruction that can be executed first, and you can also use $( )
() in the middle is the start and end of the subshell
{} in the middle is the combination of the command block


Regular Notation (regular expression)

The method of processing strings, he performs string processing behaviors in units of behavior. With the help of some special symbols, users can easily achieve the process of "searching/deleting/replacing" a specific string!


. There must be an arbitrary character (except newline)
* Repeat the previous character 0 to infinite times (wildcards represent 0 to any number of characters)
{} Repeat characters within a certain range. {} has a special meaning in the shell and needs to be converted when used. For example: $last | grep -n 'o\{1,\}'
^ Beginning of line
$ End of line
\ Escape
[] [az] Choose one
[^az] Negate

Note the difference with wildcards. In commands (tools) that do not support regular notation, it is the use of wildcards.

Extended regular notation (not applicable to sed, applicable to extensions such as gawd)
such as: remove comments and blank lines from documentation
gerp -v '^#' file_name | grep -v '^$' Use extended regular notation:
egrep -v ' ^$|^#' file_name (can also use grep -E , grep does not directly support extended regular notation)
Extended regular notation can be searched through the group function ( | pipeline command), ' | ' in single quotes The inside is or means that the 

extended regular notation is a few more special symbols.
+ Repeat one or more previous characters, o+ represents more than one o
? Zero or 1
| or means, such as finding gd or good: egrep -n 'gd|good'   
() find group characters,
()+ multiple groups (repeated)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326749394&siteId=291194637