Regular Exdivssion (RE) 1 What is a regular notation: What is a regular expression (Regular Exdivssion, hereinafter referred to as RE)? To put it simply, in the Linux environment, we can use the "string and some special characters to help" the text comparison, so that users can filter the data they need. These special characters and the tools used together form the main axis of the regular representation! For example, the /etc/rc.d/init.d directory is fine. If you want to find a file name containing the string name of the mail, how do you search? Use grep with mail and universal characters to search for all file names "grep 'mail' /etc/rc.d/init.d/*" 2 Formal representation for system administrators: For system administrators, The formal expression is a good thing to learn! 』Because the system is in a busy situation, the message generated every day will be as high as you can't imagine, and we all know that the system's "error message login file" records all the information generated by the system, of course. This includes record data of whether your system has been "invaded." However, the amount of data in the system is too large. It is very difficult for the system administrator to look at so much information data every day. It is very difficult to think about it. At this time, we can use the function of "regular representation". The logged-in information is processed, and only the "error" information is taken out for analysis. 3 Wide use of regular notation: In addition to the system administrator, a large number of software and settings support the formal representation, the most common example is the "mail server"! Do you often receive the most criticized "advertising letters" in emails? Then if I remove the advertisement letter on the server side, the client will reduce a lot of unnecessary bandwidth loss, right? So how do you get rid of advertising letters? Since the advertisement letter has almost certain titles or contents, as long as there is a letter, the title and content of the letter will be compared with the special string, and the normal expression will be used to find out that there are bad letters to be excluded! At present, the two major server softwares sendmail and postfix support the normal representation comparison function! Many server software and suites support regular notation. 4 grep Syntax: [root @test /root ]# grep [-acinv] 'Search String' filenames-list Parameter Description: -a : Search binary file as text file -c : Calculate find 'search string The number of times -i : ignores the difference between uppercase and lowercase, so the case is considered the same -n : By the way, the output line number -v: reverse selection, that is, the line showing no 'search string' content! Example: [root @test /root]# grep 'root' /var/log/secure Search /var/log/secure This file contains the root line [root @test /root]# grep -v 'root' /var /log/secure Search for lines without root [root @test /root]# grep [AZ]ANPATH /etc/man.config Description: grep is a very common instruction, the most important function is to compare string data. What needs to be explained is that when grep searches for a string in a file, he uses the "whole line" as the unit to retrieve the data! Grep is one of the simplest regular notation search instructions. He does not support some of the more rigorous regular notation content, but it is quite easy to use. Example 1: Find out that this file contains the character know and list the line number: Note that the case is not the same [root @test /root ]# grep -n 'know' regexp.txt Example 2: Find out This file contains the * character and lists the line number: [root @test /root ]# grep -n '\\*' regexp.txt Example 3: I want to list all the knows, regardless of capitalization, and list the lines. No.: [root @test /root]# grep -ni 'know' regexp.txt Note: Similar commands include egrep, awk, gawk, sed, etc., and the special characters (charaters) of the 5 regular notation will be explained in detail later. The special characters with the egrep instruction indicate the meaning of the word ^word The character to be searched at the beginning of the line word$ The character to be searched at the end of the line. Match any of the possible characters\\ Jumping symbols to turn special characters into ordinary characters? Any "single" character * Repeated characters in the matching pattern [list] Characters in the list [range] Characters in the range in the list [^list] Reverse selection, opposite to [list] [^range] Reverse selection, contrary to [range]\\ {n\\} floating the same word as the previous one \\{n,m\\} floats consecutively with the same word in the last nm. Please note that the "special characters of the regular notation" are not the same as the "universal characters" that are normally entered in the command line. For example, Among the universal characters, * represents the meaning of 0 ~ an infinite number of characters, but in the regular notation, * is the meaning of repeating the previous character - the meaning of the use is not the same, do not confuse! Example: Under /etc, as long as the line containing any one of the three characters of XYZ is listed as grep [XYZ] /etc/* Example: I want to know that in /etc, as long as the first sentence is wz He printed it? Grep ^[w-z] /etc/* 6 diff Compare the contents of two files for inconsistent instructions! Syntax: [root @test /root ]# diff file1 file2 Example: [root @test /root]# diff index.htm index.html Example: ls –l