Awk is an excellent text processing tool, one of the most powerful data processing engines available in Linux and UNIX environments.
This blog is Http://coolshell.cn/articles/9070.html's reading notes.
---
//
Output by column
//
First, use
Netstat > Netstat.txt
command to redirect netstat results to Netstat.txt
The result looks something like this:
Proto recv-q send-q Local address Foreign address State
TCP 0 0 localhost:37745 localhost : 4730 established
TCP 0 0 localhost:48344 localhost:4730 established
TCP 0 0 localhost:4730 localhost:48344 established
TCP 0 0 localhost:48342 localhost:4730 established
TCP 0 ip-10-156-238-114.a:ssh li476-220.members:49359 Established
TCP 0 0 localhost:4730 localhost:37741 established
TCP 0 0 localhost:48340 localhost:4730 established
TCP 0 0 localhost:48341 localhost:4730 established
TCP 0 1 ip-10-156-238-114.:http 60.175.244.116:5268 fin_wait1
Now, I'm going to print the first column ($) and the fourth column ($), note that the whole line is $
awk ' {print $, $} ' Netstat.txt
The results are as follows:
Proto local
TCP localhost:37745 TCP
localhost:48344 tcp
localhost:4730 TCP
localhost:48342
TCP Ip-10-156-238-114.a:ssh TCP
localhost:4730 tcp
localhost:48340 TCP
localhost:48341
TCP Ip-10-156-238-114.:http
//
Filter Records
//
The following filter conditions are: The value of the third column is 0 && the value of the 6th column is established
awk ' $3==0 && $6== ' established ' netstat.txt
Results:
TCP 0 0 localhost:37745 localhost:4730 established
TCP 0 0 localhost:48344 localhost:4730 established
TCP 0 0 localhost:4730 localhost:48344 established
TCP 0 0 localhost:48342 localhost:4730 established
TCP 0 0 localhost:4730 localhost:37741 Established
//
Built-in variables
//
$ current record (the contents of the entire row are stored in this variable)
$1~ $nNth field in the current record, separated by FS
FSThe input field delimiter is by default a space or tab
NF the number of fields in the current record, that is, how many columns
NR
The number of records that have been read is line number, starting from 1, if there are more than one file, this value is constantly accumulating.
FNR current record number, unlike NR, this value will be the individual file's own line number
The record delimiter entered by RS, which defaults to line breaks
OFS
Output field separator, default is also a space
ORS the record separator for output, which defaults to line breaks
FILENAME the name of the current input file
Examples of output line numbers:
awk ' $3==0 && $6== "established" | | nr==1 {printf "%2s%-20s%-20s%s\n", NR, $, $, $} ' Netstat.txt
Results:
1 (w/o servers)
3 localhost:37745 localhost:4730 established
4 localhost:48344 localhost : 4730 established
5 localhost:4730 localhost:48344 established
6 localhost:48342 localhost:4730 established
8 localhost:4730 localhost:37741 established
9 localhost : 48340 localhost:4730 established
localhost:48341 localhost:4730
established localhost:4730 localhost:37747 established
//
Specify separator
//
awk ' begin{fs= ': "} {print $, $ $} '/etc/passwd
Description
Begin{This inside is the statement before execution}
End {This contains the statement to be executed after all the rows have been processed}
Results:
Root 0/root
bin 1/bin
daemon 2/sbin
adm 3/VAR/ADM
LP 4/VAR/SPOOL/LPD
sync 5/sbin
Another way to write the awk command above is:
Awk-f: ' {print $, $ $} '/etc/passwd
//
Regular match
//
awk ' $ ~/est/| | nr==1 {print NR, $, $, $} ' ofs= ' \ t ' netstat.txt
The $ ~/est/portion of the above commands is a regular match. Represents the start of a regular, ~ representation pattern for the 6th column. //medium is the pattern.
1 (w/o servers)
3 localhost:37745 localhost:4730 established
4 localhost:48344 localhost:4730 established
5 localhost:4730 localhost:48344 established
6 localhost:48342 localhost:4730 established
//
Statistics
//
Lists the size of all PHP files in the current directory (that is, the fifth column of the ls-l result)
Ls-l *.php | awk ' {print $} '
Calculates the total size of all PHP files under the current directory
Ls-l *.php | awk ' {sum+=$5} end {print sum} '
Statistics of individual connection States (note the use of arrays in them)
awk ' nr!=1{a[$6]++;} End {for (I in a) print I "," a[i];} ' Netstat.txt
To count the amount of memory consumed by each user's process
PS aux | awk ' nr!=1{a[$1]+=$6} end {for (I in a) print I, ' a[i] ' KB '} '