Linux Three Musketeers awk, SED, grep
1, awk is mainly used to do the breakdown, matching, calculation work
2, SED is mainly used to do the replacement (stream processing)
3, grep is mainly used to filter out the required text
awk (the default delimiter is a space) awk-f ":" '/bonzi/{if (nr==2) print $} ' (General mode)
-F Set delimiter, you can set multiple delimiters-F "[:!] +"
NR denotes line number
$NF indicates a trailing line
NF number of fields in the current record
Built-in functions
Gsub (R,s) |
Use s instead of R for the entire $ |
Gsub (r,s,t) |
Replace R with S in the whole t |
Index (S,T) |
Returns the first position of the string T in S |
Length (s) |
return s length |
Match (S,R) |
Tests if s contains a string matching R |
Split (S,A,FS) |
Dividing s into sequence a on FS |
Sprint (FMT,EXP) |
Returns the EXP formatted by FMT |
Sub (r,s) |
Replace s with the longest substring in the leftmost string |
SUBSTR (S,P) |
Returns the suffix part of the string s starting from P |
SUBSTR (S,p,n) |
Returns the suffix part of the string s from p starting at length n |
Sed (stream edit) sed-n ' 2s# Original # change #g ' file
-N cancels the default output
-I modify Content
-E allows multiple edits
-R Extended Regular
Sed-n '/bonzi/s# Original # change #gp ' file
Sed-i ' 2,5s# Original # change #g ' file
Examples of latter references:
[Email protected] test]# ifconfig eth1|sed-nr ' 2s#.*addr: (. *) BC.*$#\1#GP '
192.168.1.8
grep (filter text)
-V Exclusion
-I is case insensitive
-o outputs only matching content
-A print matches to the following line,-a 3 prints after three lines
-B Prints the preceding line that matches to,-B 4 prints the first 4 lines
-C print the line that matches to the context, and-C 5 prints the top and bottom 5 lines
Linux Three Musketeers awk, SED, grep