Awk command

  

1.awk workflow
a) first execute BEGIN; b) read the file, read a record with /n newline splitting c) divide the record by the specified domain separator d) padding field, where $0 means all fields, $1 means the first field, $n means the first field (the default field separator is a blank key or a tab key) e) the action corresponding to the start execution mode action f) then starts Read the second record until all the records have been read g) Execute the END operation

2. Use the method
awk [-F field-separator] '{pattern + action}' {filenames} 1. Pattern represents what awk looks for in the data, and action represents a series of commands that are executed when a match is found. Action{} can have multiple statements separated by a ";". 2. [-F field separator] is optional. Filenames is the file to be processed. In awk, in each line of a file, each item separated by a domain separator is called a field. Typically, in the case of the unnamed -F domain separator, the default domain separator is a space.

3.awk built-in variable
ARGC command line parameter number ARGV command line parameter arrangement ENVIRON support queue system environment variable use FILENAME awk browse file name FNR browse file record number FS set input field separator, Equivalent to the command line -F option NF Browse the number of fields in the record NR Number of records read OFS Output field separator ORS Output record separator RS Control record separator

4.awk built-in string function
Gsub(r,s) replaces rgsub(r,s,t) with s throughout $0. Replaces rindex(s,t) with s throughout t. Returns the first position of string t in s length(s) returns s The length match(s,r) tests whether s contains the string matching the r (str,array,fs). On ss, divide s into sequences asprint(fmt,exp). Return the expsub(r,s) formatted by fmt. Replace shsubt(s,p) with the leftmost longest substring of $0. Return the suffix part of the string s from p to the substr(s,p,n). Return the suffix part of the string s starting from p and having a length of n.

5.BEGIN and END
BEGIN indicates that the operation END before processing any line indicates the processing performed after all the input lines have been processed.

6. Conditional statement

if (expression) { statement; statement; ... ...ifif (expression) { statement;} else { statement2;}if (expression) { statement1;} Else if (expression1) { statement2;} else { statement3;}

7. Array
Because the subscript of an array in awk can be numbers and letters, the subscript of an array is often called a key. Both the value and the keyword are stored in a separate table for the key/value application hash. Since hashes are not stored sequentially, you will find that when displaying array contents, they are not displayed in the order you expect. Arrays, like variables, are created automatically when they are used. Awk also automatically determines whether it stores numbers or strings. In general, arrays in awk are used to collect information from records, which can be used to calculate sums, count words, and how many times the tracking template is matched.

awk -F ':' 'BEGIN {count=0;} {name[count] = $1;count++;}; END{for (i = 0; i < NR; i++) print i, name[i ]}' /etc/passwdrootdaemonbinsyssyncgames......awk -F'\\t' '{ a[$1]++ }'END'{ for(j in a) print a[j],j }' filename |
  Sort -rn |
  The content of more filename is: apple banana apple apple banana pear output: 3 apple 2 banana 1 pear
						
Copyright © Windows knowledge All Rights Reserved