The use of grep and regular expressions
1. Common option:-E: turn on the regular expression of the Extend. -I: ignore case (ignore case). -v: on the contrary (invert), only prints no matches, and matches do not print. -n: display line number-w: the text to be matched can only be a word, not a certain part of the word, such as liker in the text, and I am only searching for like, you can use the-w option to avoid matching liker-c: show the total number of lines that have been matched, rather than show the content that has been matched, note that if you use the-cv option at the same time, it shows how many lines have not been matched. -o: only the strings matched by the pattern are displayed. -- color: highlights the matching content in color. -A n: displays the line of the matched string and the following n lines, after-B n: shows the line of the matched string and its first n lines, before-C n: shows the line of the matched string and its n lines before and after it, context2. The tom in the middle of the common expression parameter\ btom\ b "\ b" is a word\ d for a number\ d {2} -\ d {8}. The {2} ({8}) after\ d means that the previous\ d must be repeated 2 times in a row (8 times). \ s matches any space characters, including spaces, tabs (Tab), newline characters, Chinese full-width spaces, etc. \ W matches letters or numbers or underscores or Chinese characters, etc. . Match any character except newline character\ d match number\ b match the beginning or end of a word ^ match the beginning / / ^ and $of the string match the line $match the end of the string ^\ d {5pm 12} $fill in the QQ number must be 5 to 12 digits Note: "\ d" and other characters are only available in perl {regular Escape can be used in supported relative integrity}):\ character repetition): * repeat zero or more times + repeat one or more times? Repeat zero or once {n} repeat n times {n,} repeat n or more times {nMagnem} repeat n to m times basic usage
Cat 123.txt | grep'\ bno\ b'
#\ b make a prefix or suffix, which means that only no characters are matched, not even one more
Cat 123.txt | grep'^ root.400 $'
# Mou sets the beginning of the line and $Mou sets the end of the line, indicating that it matches the lines that begin with root and end with 400.
Echo "111222333" | grep-o 1.2
# display only matching words
Cat / etc/passwd | grep-n-A 1'^ root.*'
# shows the root line, the next line, and the line number.-An is replaced by-B is the previous line, and-C is replaced by one line from top to bottom.
Matching number
Echo "ABC123abc" | grep-o [0-9] | xargs
# matches the numbers. Due to the grep feature, the displayed numbers will be displayed vertically, and the pipeline will be sent to xargs for processing.
Cat cs/testfile.txt | grep-x-n test #-x shows only the exact matching line, and the effect of-n plus line number is similar to that of sed-n'/ ^ test$/='
Echo "2002 1945 1644 1892 1946" | grep-P'\ b (20 | 19) [0-9] {2}\ b'
# means the last two characters starting with 20 or 19 are numbers. {2} means that the first [0-9] must match twice, at least four digits, but two\ b Mouding prefixes and suffixes can only be four digits.
Advanced usage
Echo "http://www.ylc520.com/"|grep-oP'. + (? =: / /)'
# output result http, indicating that the match is delimited by: / /, and. + indicates that any character appears one or more times.
Echo "http://www.ylc520.com/"|grep-oP'(?