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
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 printing
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
Three. Awk intercepts
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 filed) 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
Extended reading: Extended reading:
1. grep can actually be used in this way:
Filter all the lines in the *.php document that contain eval in a directory
Grep-r--include= "*.php" ' eval '/data/
Practical application: See if your website program has been hacked http://www.lishiming.net/thread-1028-1-1.html
2. awk Prints single quotation marks with print http://www.lishiming.net/thread-1738-1-1.html
3. Print the contents of a particular line in a file to a line http://www.lishiming.net/thread-559-1-1.html
4. Use the external shell variable http://www.lishiming.net/thread-199-1-1.html in awk
5. awk merges a file http://www.lishiming.net/thread-493-1-1.html
6. Grep-q for if logic judgment http://www.lishiming.net/thread-439-1-1.html
7. awk calculates 1 to 100 of the and http://www.lishiming.net/thread-384-1-1.html
8. Concatenate a file into one line http://www.lishiming.net/thread-266-1-1.html
9. Use of the Gsub function in awk http://www.lishiming.net/thread-200-1-1.html
Sed to add a number to a line at the end of a file http://www.lishiming.net/thread-288-1-1.html
awk intercepts specifying multiple fields as one row http://www.lishiming.net/thread-224-1-1.html
SED deletes the next line of a keyword to the last line http://www.lishiming.net/thread-213-1-1.html
grep or Egrep or awk filters two or more keywords http://www.lishiming.net/thread-198-1-1.html
14. Use awk to write a program that generates the following structure files http://www.lishiming.net/thread-5494-1-1.html
15. How to use SED to print rows from 1 to 100 lines containing a string http://www.lishiming.net/thread-1048-1-1.html
16. Merge the same rows from the two files into one line http://www.lishiming.net/thread-945-1-1.html
. sed conversion Case http://www.aminglinux.com/bbs/thread-7758-1-1.html
awk Tutorial Http://www.cnblogs.com/emanlee/p/3327576.html
2015-04-10/2015-04-13 Regular