Awk:
Awk is a powerful text analysis tool, relative to Grep's lookup, the SED editor,
Awk is especially powerful when it comes to analyzing data and generating reports.
To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.
AWK has 3 different versions: AWK, Nawk, and gawk, which are not specifically described, generally referred to as the GNU version of awk, Gawk,gawk.
Awk has its name from the first letter of its founder Alfred Aho, Peter Weinberger and Brian Kernighan's surname.
In fact, Awk does have its own language: The awk programming language, a three-bit creator has formally defined it as "style scanning and processing language."
It allows you to create short programs that read input files, sort data, manipulate data, perform calculations on input, and generate reports, as well as countless other features.
Awk Workflow:
Reads a record with the "\ n" newline character split, and then divides the records into fields by the specified domain delimiter, populating the fields
$ A represents all fields, representing the first domain, and so on
The default Domain delimiter is a blank key or TAB key
-F: Specify the domain delimiter
cat /etc/passwdawk -F":"'{print $}'
The middle can be filled with \ t.
cat /etc/passwdawk -F":"'{print $ 1 "\ T" $7}'
When there is a begin,end, the awk workflow:
Begin, then read the file, read into the first record with a newline character split, and then install
The specified domain delimiter divides the fields, fills the fields, and $ A represents all the fields, representing the first domain, and so on
The action action corresponding to the execution mode is then started
Final execution, last end operation
cat /etc/passwdawk -F':'BEGIN {print ' username Bash "} {print $", "$7} END {print" Run Over "}'
The output needs to be included with "", where begin,end are keywords and must be capitalized
awk built-in variables:
Awk also defines a lot of built-in variables, and we can use them just like normal variables, and because of the many versions of awk, some built-in variables are not supported by all versions of awk.
Description: [A][n][p][g] represents a tool that supports this variable, [A]=awk, [N]=nawk, [P]=posixawk, [G]=gawk
$n the nth field of the current record, for example, N is 1 for the first field, and N for 2 for the second field. $0 This variable contains the text content of the current line during execution. [N] The number of ARGC command-line arguments. [G] Argind the location of the current file in the command line (starting at 0). [N] ARGV contains an array of command-line arguments. [G] CONVFMT number conversion format (default is %. 6g). [P] ENVIRON an associative array of environment variables. [N] ERRNO The description of the last system error. [G] fieldwidths field width list (separated by Space key). [A] The name of the filename of the current input file. [P] FNR with NR, but relative to the current file. [A] FS field delimiter (default is any space). [G] IGNORECASE If true, the matching of the case is ignored. [A] NF represents the number of fields, which corresponds to the number of fields in progress during execution. [A] NR represents the number of records, which corresponds to the current line number during execution. [A] The output format of the OFMT number (the default value is %. 6g). [A] OFS the output field delimiter (the default value is a space). [A] ORS output record delimiter (default value is a newline character). [A] RS record delimiter (default is a line break). [N] Rstart the first position of a string that is matched by the match function. [N] Rlength the length of the string that is matched by the match function. [N] subsep array subscript delimiter (the default value is 34).
AWK Programming:
Variables and Assignments
Print is just a statement
awk ' BEGIN {count=0} {count++;p rint} END {print "User count is", count} ' /etc/passwdlsgrepawk'begin{size=0} {size=size+$5} END {Print size} '
Conditional statements:
if (expression) {statement;statement; ...} if Else {statement;} if Elseifelse {statement;}
Statistics directory file size, filter out 4096 (usually folders)
ls awk ' BEGIN {size=0;} {if ($5!=4096) {size=size+$5}} END {print "size is:", size}'
Loop statement:
While loop
awk ' BEGIN {count=0;while (count<5) {print count;count++;}} '
Do...while Cycle
awk ' BEGIN {count=0;do{print count;count++;} while (COUNT<5)}
For loop
awk ' BEGIN {for (count=0; count<5; count++) {print Count}} '
Using the array summation, for...in loops
awk ' {sum[$1]+=$2}end{for (k in sum) {print K "" Sum[k]}} '
Awk built-in functions:
Arithmetic:
ATAN2 (y,x) returnsthe arc tangent of y/X. COS (x) returns the cosine of x, and X is radians. Sin (x) returns the sine of x, and X is radians. EXP (x) returns the X-Power function. Log (x) returns the natural logarithm of x. SQRT (x) returns the square root of x. int01. Srand ([expr]) sets the seed value of the RAND function to the value of the expr parameter, or if the expr parameter is omitted, the time of day is used. Returns the previous seed value.
String:
gsub (REG,STR1,STR2) uses STR1 to replace all the substrings in the str2 that conform to the regular expression Reg sub (REG,STR1,STR2) mean the same as gsub, except that Gsub replaces all matches, and the sub replaces only the first matching IND EX (STR,SUBSTR) returns the index of the first occurrence of substr in STR, noting that the index is calculated starting from 1, and if not, returns 0 length (str) to return the lengths of the STR string, which can also return the number of array elements blength (str) Returns the number of bytes in a string match (Str,reg) is the same as the index function except that Reg uses regular expressions, such as match ("hello",/lo/) Split (Str,array,reg) to save the STR-delimited array to an array, separating the use of regular Reg, or the string can be, returning the ToLower (str) converted to lowercase toupper (str) to uppercase substr (str, Start,length) intercepts a string, the length of a character starting at the start index, or, if unspecified, the end of the index, starting at 1
Other:
DD HH MM ss[DST]) Generate Time Format strftime (format,timestamp) format time output, convert timestamp to time string systime () get timestamp, return start from January 1, 1970 to current time (regardless of leap year ) The number of full seconds
Reference: https://www.cnblogs.com/wangqiguo/p/5863266.html
Linux awk detailed