May 5, 2016 regular expressions

Source: Internet
Author: User

Content Summary

Regular is a certain pattern of strings, there are several special symbols are key (. * + ? | , we can not only use the command line tool Grep/sed/awk to refer to the regular, but also can be embedded in Nginx, Apache, and even PHP, Python programming language, learn the regular can let you enjoy endless!
First, Grep/egrep
1. Syntax + options
Syntax: grep [-CINVABC] ' word ' filename
-C: Print the number of lines that meet the requirements
-N: Output with the same number of lines as required
-V: Print rows that do not meet the requirements
-A: followed by a number (with or without spaces), for example, –A2 to print the line that meets the requirements and the following two lines
-B: followed by a number, such as –B2, to print the line that meets the requirements and the above two lines
-C: followed by a number, such as –C2, to print the line that meets the requirements and two rows above and below
-R: All files under the directory are traversed

2. Example Introduction
Filter out a line with a keyword and enter the travel number grep-n ' root ' 1.txt
Filter out lines without a keyword and lose travel number grep-n-V ' root ' 1.txt
Filter out all rows that contain numbers grep ' [0-9] ' 1.txt
Filter out all lines that do not contain numbers grep-v ' [0-9] ' 1.txt
Remove all lines beginning with ' # ' grep-v ' ^# ' 1.txt
Remove all empty lines and lines beginning with ' # ' grep-v ' ^$ ' 1.txt|grep-v ' ^# '
Filter out lines that begin with the English alphabet grep ' ^[a-za-z] ' 1.txt
Filter out lines that start with a non-digit grep ' ^[^0-9] ' 1.txt
Filter any one or more characters grep ' r.o ' 1.txt; grep ' r*t ' 1.txt; grep ' R.*t ' 1.txt
. Represents any one character; * represents 0 or more preceding characters; * denotes 0 or more arbitrary characters, and a blank line is included
Specify the number of filter characters grep ' o\{2\} ' 1.txt

3. Egrep
The Egrep tool is an extension of the grep tool
Egrep ' o+ ' 1.txt means 1 or more than 1 preceding characters
Egrep ' O? ' 1.txt represents 0 or 1 preceding characters
Egrep ' roo|body ' 1.txt match roo or match body
Egrep ' R (oo) | (at) o ' 1.txt brackets denote a whole
Egrep ' (oo) + ' 1.txt denotes 1 or more ' oo '

4.. * + ? Summarize
. Represents any one character (including special characters)
* Represents 0 or more * preceding characters
. * denotes any arbitrary character (contains blank lines)
+ represents 1 or more + preceding characters
? Represents 0 or 1 characters in front of each other
where, +? GREP is not supported, EGREP is supported.

Two. sed
Print the specified line sed ' p-n 1.txt; Sed ' 1,4 ' p-n 1.txt; Sed ' 5,$ ' p-n 1.txt
Print the line containing a string sed-n '/root/' p 1.txt can use ^. * $ and other special symbols
-E can implement multiple tasks simultaneously sed-e '/root/p '-e '/body/p '-n 1.txt can also be used; realize sed '/root/p; /body/p '-N 1.txt
Delete Row sed '/root/d ' 1.txt; Sed ' 1d ' 1.txt; Sed ' 1,10d ' 1.txt
Replace sed ' 1,2s/ot/to/g ' 1.txt, where S is the meaning of the substitution, G is the global substitution, otherwise only the first time,/can also be #, @, etc.
Remove all digital sed ' s/[0-9]//g ' 1.txt
Remove all non-digital sed ' s/[^0-9]//g ' 1.txt
Swap two string positions head-n2 1.txt |sed ' s/\ (root\) \ (. *\) \ (bash\)/\3\2\1/'
Modify file contents directly sed-i ' s/ot/to/g ' 1.txt

Sed Practice Questions:
Copy/etc/passwd to/root/test.txt, print all lines with SED
Print 3 to 10 lines of test.txt
Print the line containing ' root ' in test.txt
Delete Test.txt 15 rows and all subsequent rows
Delete rows containing ' bash ' in Test.txt
Replace ' root ' as ' Toor ' in Test.txt
Replace Test.txt '/sbin/nologin ' as '/bin/login '
Delete all the numbers in rows 5 through 10 in Test.txt
Delete all special characters in test.txt (except for numbers and uppercase and lowercase letters)
Swap the first and last words in the test.txt position
Replace the first number and the last word that appear in the test.txt position
Move the first number in the Test.txt to the end of a line
Test.txt 20 to the end of the line Plus ' AAA: '


Three. awk
Intercept a section of the document Awk-f ': ' {print $} ' 1.txt
You can also use a custom character to connect each segment Awk-f ': ' {print $ "#" $ "#" $ $ "#" $4} ' 1.txt
Match character or string awk '/oo/' 1.txt
Match awk-f ': ' ~/oo/' 1.txt for a segment
Multiple matches awk-f ': '/root/{print $1,$3}; $ ~/test/; $ ~/20/' 1.txt
Conditional operator = =, >,<,!=,>=;<=
Awk-f ': ' $3== ' 0 "' 1.txt;
Awk-f ': ' $3>= ' 1.txt;
Awk-f ': ' $7!= '/sbin/nologin ' 1.txt;
Awk-f ': ' $3<$4 ' 1.txt;
Awk-f ': ' $3> ' 5 "&& $3<" 7 "' 1.txt
Awk-f ': ' $3> ' 5 ' | | $7== "/bin/bash" ' 1.txt
awk built-in variable NF (number of segments) NR (number of lines)
Head-n3 1.txt | Awk-f ': ' {print NF} '
Head-n3 1.txt | Awk-f ': ' {print $NF} '
Head-n3 1.txt | Awk-f ': ' {print NR} '
Print 20 lines after the line awk ' nr>20 ' 1.txt
Awk-f ': ' nr>20 && ~/ssh/' 1.txt
Change the value of a segment awk-f ': ' $1= ' root ' 1.txt
Mathematical calculations, add the third and fourth values and give the seventh paragraph Awk-f ': ' {$7=$3+$4; print $} ' 1.txt
Calculates the sum of the third paragraph awk-f ': ' {(tot=tot+$3)}; END {print tot} ' 1.txt
Awk can also use if keyword awk-f ': ' {if ($1== "root") print $} ' 1.txt

awk Exercises
Print the entire test.txt with awk (the following is done with the awk tool for Test.txt)
Find all rows that contain ' bash '
Use ': ' as a delimiter to find a line with a third paragraph equal to 0
Use ': ' as a delimiter to find the first line of ' root ' and replace the ' root ' of that segment with ' Toor ' (which can be used together with SED)
Use ': ' As a delimiter to print the last paragraph
Print all rows with a number of rows greater than 20
Use ': ' As a delimiter to print all third paragraphs less than the fourth paragraph
Use ': ' As a delimiter, print the first paragraph and the last paragraph, and the middle with ' @ ' connection (for example, the first line should be the form of ' [email Protected]/bin/bash ')
Use ': ' As a delimiter to add the fourth paragraph of the entire document, summing

May 5, 2016 regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.