Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which 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.
How to use: awk ' {pattern + action} ' {filenames}
Although the operation can be complex, the syntax is always the same, where pattern represents what AWK looks for in the data, and the action is a series of commands that are executed when a match is found. Curly braces ({}) do not need to always appear in the program, but they are used to group a series of instructions according to a particular pattern. pattern is the regular expression to be represented, surrounded by slashes.
The most basic function of the awk language is to browse and extract information in a file or string based on the specified rules, before awk extracts the information for additional text operations. A complete awk script is typically used to format the information in a text file. Typically, awk is treated as a unit of a file's behavior. awk processes the text by executing the corresponding command for each line that receives the file.
awk built-in variables
ARGC command-line arguments argv command-line parameter arrangement environ support the use of system environment variables in queues filename awk browses the file name Fnr the number of records to browse files FS Set input field delimiter, equivalent to command line-F option NF Browse record number of fields nr Read Record count ofs output field delimiter ors Output record delimiter Rs control record delimiter the $ variable refers to the entire record. $ $ represents the first field of the current row, which is the second field of the current row,...... And so on
$NF is number finally, which represents the last column of information, which is different from the variable NF, the variable NF counts the total of each row
Common Command Display
AWK excels at column output
Search all rows with the root keyword/etc/passwd
Awk
Search all lines that have the root keyword/etc/passwd and display the corresponding shell
Statistics/etc/passwd: File name, line number per line, number of columns per row, corresponding full line contents:
Awk- F ': ' {print ' filename: ' filename ', linenumber: ' NR ', columns: ' NF ', linecontent: ' $ '/etc/passwd
Use printf instead of print to make your code more concise and easy to read
Awk-f: ' {printf ("filename:%10s, linenumber:%3s,column:%3s,content:%3f\n", filename,nr,nf,$0)} '/etc/passwd
Print the second line of/etc/passwd/information
Awk-f: ' nr==2{print ' filename: ' filename, $ A} '/etc/passwd
How Awk uses the filter
LS-LF | awk '/^d/'
Specify a specific delimiter to query the first column
Awk-f ":" ' {print $} '/etc/passwd
Specify a specific delimiter to query the last column
Specify a specific delimiter, query the second-to-last column
Awk-f ":" ' {print $NF-1} '/etc/passwd
Gets the information for the first column of row 12th to 31st
Awk-f ":"
Use of multiple separators:
[[email protected] ftl]# awk-f "[/]" ' NR = = 4 {print $, ' \ n ', ' $ '/etc/passwd here with/as delimiter, multiple separators using [] and then write the delimiter inside
Begin and end are added
[Email protected] ftl]# CAT/ETC/PASSWD | Awk-f: ' begin{print ' name, Shell '} {print $, $NF} end{print ' Hello World '} '
View the most recently logged on IP information
AWK Programming--
Variables and Assignments
In addition to Awk's built-in variables, awk can also customize variables, and the looping statements in awk also draw on the C language, supporting while, Do/while, for, break, continue, which have the same semantics as the C language.
Count the number and sum of files larger than 100k in a folder
Ls-l|awk ' {if ($5>100) {count++; sum+=$5}} {print "Count:" Count, "sum:" Sum} ' "because awk polls the statistics, the entire process is displayed" Ls-l|awk ' {i F ($5>100) {count++; sum+=$5}} end{print "Count:" Count, "sum:" Sum} ' "The final result is displayed only after the end of the day"
Note:count is a custom variable. The previous action{} is only one print, in fact, print is just a statement, and action{} can have multiple statements, separated by a number;
Statistics show/ETC/PASSWD's account
Awk-f: ' {count++;} End{print count} '/etc/passwd cat/etc/passwd|wc-lawk-f ': ' BEGIN {count=0;} {Name[count] = $1;count++;}; End{for (i = 0; i < NR; i++) print I, Name[i]} '/etc/passwd
Linux awk Command Details