1 What is awk is not only a text processing command, it is also a programming language. It can process columns without affecting the original file. 2 simple usage of awk $ awk-F: '{print $1}'/etc/passwd $ awk-F: '{print $1, $3} '/etc/passwd $ awk-F:' {print $1 "|" $3} '/etc/passwd $ awk-F: '/root/{print $1 "|" $3}'/etc/passwd $ awk-F: '/^ root/'/etc/passwd $ awk-F: -f test. awk/etc/passwd 3 awk script syntax awk 'in in {actions}/pattern1/{actions }...... /patternN/{actions} END {actions} 'input_file 1) Where BEGIN {actions} and END {actions} are optional. Note that both BEGIN and END are uppercase letters. 2) awk_script can be composed of one or more awk_cmd entries, each of which occupies one row. 3)/pattern/and {actions} In awk_cmd can be omitted, but cannot be ignored at the same time;/pattern/indicates executing the specified actions for all input rows; if {actions} is omitted, the entire row is printed. 4 awk Execution Process ① if BEGIN exists, awk first executes the specified actions ② awk to read a row from the input, which is called an input record ③ awk splits the read records into several fields, add the first field to the variable $1, the second to the variable $2, and so on. $0 indicates the entire record. The field separator can be specified by option-F, otherwise, the default Delimiter is used. ④ Compare the current input records with the pattern in each awk_cmd in sequence: If they match, the corresponding actions will be executed. If they do not match, the corresponding actions will be skipped, wait until all the awk_cmd ⑤ After processing an input record, awk reads the next line of the input, repeat the above processing process until all the input is processed. ⑥ If END exists after awk finishes processing all input, execute the corresponding actions 7 if the input is a file list, awk will process each file in the list in order. 5 sample print ip $ ifconfig | awk '/inet addr/{print $2}' | awk-F: '{print $2}' $ ifconfig | awk '/inet addr/{print $2}' | awk-F: 'In in {print "BEGIN .. "} {print $2} 'end {print" END... "} 6 Pattern Matching 1) use regular expression 2) use a Boolean (comparison) expression. When the expression value is true, perform the corresponding operation (actions) variables (such as field variables $1 and $2) and operators in/rexp/expressions can be used as Relational operators: <><===! = Matching OPERATOR :~ !~ X ~ /Rexp/returns true if x matches/rexp/. x !~ /Rexp/returns true if x does not match/rexp. $ Awk '$1> 20 {print $0}' test. in $ awk '$2 ~ /^ 6/{print $0} 'test. in 3) composite expression: & (logical and), | (logical or ),! (Logical) expr1 & expr2 returns true expr1 if both expressions are true | expr2 returns true if one of the two expressions is true! Returns the true $ awk when the value of the expr expression is false ($1 <20) & ($2 ~ /^ 6/) {print $0} 'test. in $ awk '($1 <20) | ($2 ~ /^ 6/) {print $0} 'test. in $ awk '! ($2 ~ /^ 6/) {print $0} 'test. in $ awk '/^ #/& amp;/# $/{print}' test. in NOTE: When there is a comparison operation in the expression, it is generally enclosed in parentheses. The field separator in the field separator awk can be specified with the-F option, the default value is space awk '{print $1}' test. inawk-F: '{print $1}' test. in can be a space or: Separate awk-F' [:] ''{print $1} 'test. in 8 redirection and pipeline $ awk '{print $1, $2> "output"}' test. in $ awk 'in in {"cal" | getline a; print a} '9 more awk1) variable internal variable awk '{print NR, $0} '# Add a row number custom variable to the file. 2) built-in function custom function awk' {print sum ($1, $2)} function sum (x, y) {s = x + y; return s} 'grade.txt 3) array $ awk' BEGIN {print split ("123 #456", arr ,"#"); for (I in arr) {print arr [I]}'