Often in contact with the Shell scripting language, you will often see a shell command followed by a bunch of messy option, similar to the sed-e "s/^[^\-]*-//" , although know that it must be a logical combination of conditions, but if not familiar with the regular expression of the person, Absolutely dazzled by the sight.
Here, learn to record some basic usage of regular expressions.
Writing is not easy, reprint need to indicate the source: http://blog.csdn.net/jscese/article/details/41645025#t7
Concept:
Regular expressions, also known as formal representations, are the methods of handling strings, which are handled by the behavior unit, and the regular representation is assisted by some special symbols,
Allows users to easily reach the search and delete replace "a specific string handler!"
Use:
Take the grep command as an example
To search for a specific string:
Grep-n ' Jscese ' test.txt
Enclose a string in single quotation marks that you want to search.
The difference between single and double quotation marks is that the contents of the single quotation mark are all treated as characters, and if there are other variable names, they are only ordinary characters, and the double quotation marks are different and have reference properties.
Use the brackets [] to search for the set character:
Grep-n ' J[sc]ese ' test.txt
the brackets represent one character, so the above statement represents a search for J
sESE J
CThe two combinations of ESE are possible.
Take the inverse "^":
grep -n ' j[^s]cese ' test.txt //Take the middle not s of grep- n ' J[^a-za-z0-9]cese test.txt // Take the middle not a-Z a-Z 0-9, only the special symbol of the line grep- n ' ^jscese ' test.txt //Represents the beginning, take the line beginning with Jscese
Use ^ in brackets [] inside and outside the meaning of the representation is not the same, in
The inside represents the reverseIn
outside represents the beginning of the line!
End of Line "$":
grep -V ' ^$ ' test.txt //Take non-empty rows
The beginning of the line ^ directly plus the end of $, representing an empty line,-V is to take a non-matching
Any one of the characters "." With the repeating character "*":
grep -n ' j....e ' test.txt ///each of them . Represents arbitrary one character grep- n ' j* ' test.txt //This will list all, because * represents repeating the preceding character 0 or countless times, that is, may be empty
Retouch the previous character
Qualifying contiguous character range {}:
grep- n ' js\{2,\} ' test.txt //{} need to skip character escapes so add \
It is also a modifier of the previous character, which means to find
JSImmediately after the opening
2More than one
sLine, \{2,6\} represents between 2 and 6
The basic rules are the above, the detailed can go to see Bird's private cuisine, now look at the beginning of the sed-e "s/^[^\-]*-//"
This is used in envsetup,sh to get the source compilation type, lunch selected the value of selection= Full_x86-eng Style
Pass:
Local variant=$ (echo-n $selection | sed-e "s/^[^\-]*-//")
You can see the direct editing via the SED command, using the SED
sInstead of the action, the following pattern is:
"s/to be replaced by the partial/substituted string/"
You can see that the part to be replaced is: ^[^\-]*- This represents starting from scratch, 0 or more non-characters then end with a-character part, because in []-has the meaning of the range, so the caret is added \
The replacement part is empty here.
So in the end, the full-x86-is actually removed, and the remaining Eng is assigned to the variant, which is the compilation mode of the engineering machine.
Getting Started with regular expressions is basically complete, with the opportunity to analyze the SED and some of the advanced uses of awk in the back-
Regular expressions in the Shell