In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Blog structure
What is a regular expression
Basic regular expression
Extended regular expression
File formatting and related processing
I. grep
A regular expression is a logical formula for manipulating a string, that is, a "regular string" is formed by predefined specific characters and a combination of these specific characters. This "regular string" is used to express a filtering logic for a string.
The use of regular expressions for system administrators
Because if the system is busy, it will generate more messages every day than you can imagine, and we all know that the contents of the system's "error message login file" record all the messages generated by the system, of course, this includes whether your system is recorded by "*"
The wide use of regular expressions
When writing programs that deal with strings, there is often a need to find strings that conform to some complex rules. Regular expressions are the tools used to describe these rules. In other words, a regular expression is the code that records text rules.
Regular expression and the role orientation of shell in linux
Whether it is the understanding of the system and the management part of the system
Extended regular expression
The string representation of regular expressions can be divided into basic regular expressions and extended regular expressions according to different rigor. In addition to simple string processing, extended regular expressions can also be used for group string processing.
Basic regular expression
1. The influence of language Family on regular expressions
The output results of zh_TW.big5 and C are as follows:
At LANG=C: 0 1 2 3 4. A B C D... Z a b c d... z
At LANG=zh_TW: 0 1 2 3 4. An A b B c C d D... Z Z
two。 A regular expression consists of three parts:
The examples are as follows:
[root@localhost ~] # grep-n "the" / etc/man_db.conf\\-n display line number search specific string [root@localhost ~] # grep-vn 'the' / etc/man_db.conf\\ reverse search means to find lines that are not' the' display [root@localhost ~] # grep-in 'the' / etc/man_db.conf\\ to get the the regardless of case When the string [root@localhost ~] # grep-n't [ae] se' / etc/man_db.conf\\ wants to search for the words test or tast You can find [root@localhost ~] # grep-n'[^ g] oo' / etc/man_db.conf\\ use brackets [] to search for the beginning of the collection character\\ ^ $end [root@localhost ~] # grep-n'[0-9]'/ etc/man_db.conf\\ get the line [root@localhost ~] # grep-n'^ the' / etc/man_db.conf\\ with a number. The ^ within the parenthesis of the trailing character ^ $indicates a reverse search for the ^ outside the parenthesis. Start with [root@localhost ~] # grep-n'^ [a murz]'/ etc/man_db.conf\\ find the beginning with the lowercase letter [root@localhost ~] # grep-n'^ [[: lower:]]'/ etc/man_db.conf\\ this is the same as the above line shows lowercase letters You can look at the figure [root@localhost ~] # grep-n'^ [^ a-zA-Z]'/ etc/man_db.conf\\ do not want the line that begins with the letter [root@localhost ~] # grep-n'\ .$'/ etc/man_db.conf\\ and ends with a decimal point (.)
In particular, it is noted that because the decimal point has other meanings, it is necessary to use the skip character (\) to remove its special meaning.
The line break character of the Windows. (. ^ M $)
The line break character of Linux. (. $)
**。 (decimal point): it means "there must be an arbitrary character"
* (asterisk): it means "repeat the previous character, 0 to infinitely many times". It is a combined form.
**
[root@localhost ~] # grep-n 'g.d' / etc/man_db.conf\\ find Gmail? D consists of four characters that begin with g and end with d [root@localhost ~] # grep-n 'ooo*' / etc/man_db.conf\\ find at least two strings of o or more\\ Note: O * is a unit, * represents repeating the preceding letter an infinite number of times [root@localhost ~] # grep-n' goo*g' / etc/man_db.conf\\ the string begins and ends with g But there can only be at least one o [root@localhost] # grep-n'[] 0-9 [0-9] *'/ etc/man_db.conf\\ between two g. Find out the row of "any number" [root@localhost ~] # grep-n'go\ {2Yue 5\} g'/ etc/man_db.conf\\ find g followed by 2 to 5 o Then follow a g string [root@localhost ~] # grep-n'go\ {2,\} g'/ etc/man_db.conf\\ to find more than 2 o goooo....g
Note: "Special characters of regular expressions" are not the same as "universal characters" that are generally used to enter instructions on the command line. For example, universal characters represent the meaning of "0 ~ infinitely many characters", but in regular expressions, the meaning of "repeating the previous RE character from 0 to infinity" is not the same. Don't mix it up.
The syntax of sed is as follows: [root@localhost ~] #] sed [- nefr] [Action] options and parameters are as follows:-n: use silent mode. In the normal use of sed, all data from STDIN is generally listed on the screen. But if you add the-n argument, only the row (or action) that has been specially handled by sed will be listed. -e: edit the actions of sed directly on the command line interface;-f: directly write the actions of sed in a file, and-f filename can perform sed actions in filename;-r:sed actions support the syntax of extended regular expressions. (the default is the basic regular expression syntax)-I: directly modifies the contents of the read file instead of being output by the screen.
Case study:
[root@localhost ~] # nl / etc/passwd | [root@localhost ~] # nl / etc/passwd\\ delete 2 to 5 lines [root@localhost ~] # nl / etc/passwd | sed 'asd'\\ add asd [root@localhost ~] # nl / etc/passwd to all subsequent lines | sed' 2a da....\ aaa'\\ add more than two lines [root@localhost ~] # nl / etc/passwd | sed'2 to replace lines 5c ccccccccccccc'\\ 5 with lines 2-5. For ccccccccc [root@localhost ~] # nl / etc/passwd | sed-n'5J 7p'\\ lists only 5 to 7 lines
The function of searching and replacing some data
Sed's / string to be replaced / new string / g'
The "- I" option of sed can directly modify the contents of the file.
Extended regular expression
III. Awk
Awk is a powerful tool, compared to the search of grep, the editor of sed, awk is particularly powerful in analyzing data and generating reports. To put it simply, awk is to read the file line by line, slice each line with a space as the default separator, and then analyze the cut part.
The syntax is as follows: [root@localhost ~] # awk 'item type 1 {action 1} condition type 2 {action 2}. ' Filenamne [root@localhost ~] # last-n 5 | awk'{print $1 "\ t" $3}'\\ withdraw the account and the login's IP, and the account is separated from the IP by [tab]
* * the processing flow of the entire awk is:
Read the first row and fill in the data of the first row with $0, $1, $2. Among the other variables; according to the restrictions of the "condition type", determine whether the following "actions" are needed; finish all the actions and condition types; if there is any subsequent "row" data, repeat the steps 1-3 above until all the data has been read.
* * [root@localhost ~] # last-n 5 | awk'{print $1 "\ t lines:" NR "\ t columns:" NF} 'root lines: 1 columns: 10root lines: 2 columns: 10 (unknown lines: 3 columns: 10reboot lines: 4 columns: 11root lines: 5 columns: 10 lines: 6 columns: 0wtmp lines: 7 columns: 7 [root@localhost ~] #\\ variables such as NR,NF in awk should be capitalized And don't need $!
Logical operation characters of awk
The logical operation above, that is, the so-called judgment of greater than, less than, equal to, etc., is traditionally expressed as "="; if it is directly given a value, such as when a variable is set, it is used directly.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.