Awk
Command format:
awk ' Begin{commands} pattern {commands} end{commands} ' file
Working mode:
1. Execute Begin{commands} statement block statement, optional statement block
2. Read a line from the file or stdin, and then execute {commands}, repeating the process until the file is all read out
3. When reading to the end of the input stream, execute the end{commands} statement block
Special variables:
Filename:awk Browse by file name
NR: Number of records, corresponding to the current line number during execution
NF: Number of fields, number of fields corresponding to the current line during execution
FS: Set delimiter, command line-F
$: text content of the current line during execution
$: The text content of the first field
$: The text content of the second field, and so on
print&printf functions for printing output
When the parameters of print are delimited by commas, the parameters are printed with spaces as delimiters, and in Awk's print statement, the double quotes are used as concatenation operators.
The printf function, whose usage is basically similar to printf in the C language, can format strings, and when the output is complex, printf is more useful and the code more understandable.
Process Control Statements
if (condition) else{}
while () {}
Do{}while ()
for (;;) such as
Built-in string control functions:
1.length (String): Returns the length of a string
2.index (string,search_string): Returns the position where the search_string appears in the string
3.substr (String,start_pos,end-pos): Creates a substring from start-pos to End-pos position in a string
4.split (String,array,delimiter): Generates a list of strings with delimiter and stores the list in an array, delimiter using the current FS value by default.
5.sub (regex,replace_str,string): Replace the first content of the regular expression with REPLACE_STR
6.gsub (regex,replace_str,string): replace regular expression to match all content
7.match (regex,string): detects if regular expressions can match strings
Application:
1. Read a line from a file or stdin and loop through {commands} until you finish reading
echo "Axbx cxdxe a;b;cx d;e" | Awk-f "[x|;|] + "' Begin{print" BEGIN: "} {for (i=1;i<=nf;++i) {++s[$i];p rintf (" $%d=%s\n ", I, $i)}} end {print" End: "; For (i in S) print I, S[i]} '
2.split function
awk for simple operation of Linux commands