A. Linux system connection Status Chapter;1, check the status of TCP connection;netstat-nat|awk ' {print$6} ' |sort|uniq-c|sort -rnnetstat-n|awk '/^tcp/{++s[$NF]}; End{for (Ains) printa,s[a]} ' or netstat-n|awk '/^tcp/ {++state[$NF]};end{for (keyinstate) printkey, "T", State[key]} ' netstat-n |awk '/^tcp/{++arr[$NF]};
Awk call Syntax:
Awk [-F
-- Where:Ch: Field-separator character.Pgm: Awk command-line program.Pgm file: File containing an Awk program.Vars: Awk variable initializations.
Data file: Input data file.
Note:
1. The data file is optional. If not specified,
Awk string function (2), awk string Function
The substr function returns a substring starting from the specified position of the string. If the length of the substring is specified, the corresponding part of the string is returned. If the specified length exceeds the actual range of the string, the actual content is returned.FormatSubstr (string, starting position)
Substr (string, starting position, substri
Awk programming model and awk Programming
It is important to understand the basic model that awk provides to programmers. Learning awk is easier than learning other programming languages because awk provides programmers with well-defined and useful models.
The
A regular expression: a regular expression (or regular expression, called re) is a text pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters). This pattern describes one or more strings to match when looking up a text body. A regular expression, as a template, matches a character pattern to the string you are searching for. Simply put, the regular expression is the method of processing the string, it is the behavior unit to cond
Nf
Total number of fields per row ($)
Nr
What awk is currently dealing with is the "number of rows" of data
Fs
Current delimited byte, default is blank key
We continue with the example above Last-n 5, if I want to:
List the account number for each line (that is, $ $);
List the number of rows currently processed (that is, t
awk Learning NotesRecently added a few features of the log, but, this log is the output, general himself also found not the problem, so want to write some simple monitoring script to see the general situation of the log,For example, there is no error, how many error reported every day. Think of the previous ops and share awk, so I want to learn simple.EntryThe simplest input some columns use $4 to indicate
operates. Its variable value is less than or equal to Nr. If the script is accessing many files,This variable is reset for each new input file.FS is used to set the domain delimiter in awk, which is the same as the-f option in the command line. By default, it is a space. If you useComma to make the field delimiter, set fs= ",".NF supports the number of record fields and then sets them after the record is r
a sign of the Code section, in this way, only the final result {print total} is output and executed once.
10. Statistics file size:
Ls-L | awk '{If (/^ [^ d]/) Total = + $5} end {print "Total KB:" Total }': /^ [^ d]/match at the beginning of the line without specifying the Domain value $1
11. awk built-in variables:
Number of argc command line parameters
Argv command line parameter arrangement
EnvironSuppo
}} ' DATA.F:
Or awk ' {if ($5>$1) {Diff=$5-$1;print $1,diff}} ' DATA.F
9, Data statistics:
awk ' {(total+=$5)}end{print total} ' data.f: ' {(total+=$5)} ' and ' {print total} ' represent two different pieces of code, and if there is no cumulative result for each of the end output, End can be understood as the flag of the code paragraph, so that only the final result, the {print total}, is executed
In addition to a logical comparison of the characters for one segment, you can logically compare two segments.example, after adding double quotes, look at the number as a character;
[Root@yonglinux ~]# awk-f: ' $3> "5" $3
example, after adding single quotes for a numeric comparison;
[Root@yong ~]# awk-f: ' $3> ' 5 ' $3
Alternatively, you can use the "and" and | | "or" mean
fileawk ' End{print FILENAME} ' datafilePrint file nameIf more than one input file can be used to determine, such as:awk ' if (filename== "File1") {arr[$3]=$1}else{($ in arr) {print arr[$1] "|" $ file1}} file2Note: awk processes the previous file before processing the subsequent fileArgindCurrently processed parameter markersARGCNumber of command line argumentsArgvCommand line parameter arrangementENVIRONSupport for the use of environment variables i
form 3-5, and multiple positions are separated by commas.Who|cut-b 3-5,8Cat/etc/passwd|head-n 5|cut-d:-F 1Use-D to set the delimiter as a colon, and then use-F to set the first domain I want to take, and then press ENTER, all the usernames are listed.Awkawk usage: awk ' pattern {action} ' awk is for each row, so it is hard escaped in the unit of behavior, "soft escape" can only escape some meta-characters,
Awk must be known to anyone who has experience processing too much text. It is a scripting language that is easy to use to process data in a certain format. The following describes common methods of awk.
If you want to learn systematically, see http://doc.lazyhack.net/awk.htm
1 awk built-in Variables
The built-in field variables of
block, which is also an optional statement blockThe General command in the pattern statement block is the most important part and is optional. If the pattern statement block is not provided, the default is {print}, which prints every fetched row, and every row that awk reads executes the statement blockawk variableVariables: Built-in and custom variablesFS: Enter field delimiter, default to white space characterAwk-v fs= ': ' {print $1,fs,$3} '/etc/p
for separate recordsRecord: Default carriage return line count one recordAwk-v rs= ";" The value in ' {print $} ' f1.txt is AA bb cc;eeFF OO;XXYY ZZ (Specifies the value of the RS variable; Print the second column of the F1.txt file, the result is BB ff yy)ORS: Specifies the delimiter for record outputAwk-v rs= ""-V ors= "# # #" ' {print} ' passwd (by default, each record is differentiated by carriage return, ORS specifies the delimiter that distinguishes each record)NF: Number of fieldsAwk-f:
' Toor ' (which can be used together with SED)[Email protected] sed]# awk-f ': ' $1==root ' |sed ' s/root/toor/g ' passwdUse ': ' As a delimiter to print the last paragraph[[email protected] sed]# awk-f ': ' {print $NF} ' passwdPrint all rows with a number of rows greater than 20[Email protected] sed]# awk ' nr>20 ' p
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.