A, awk introduced
Full name: a combination of the first letter Aho Weinberger Kernaighan three men from
In 1970 for the first time on a Unix machine, then use it in the field of open source
awk is alone programming language interpreter
awk report generator: by pattern matching language format as well as themselves, to get content and outputs the customer needs
Examples: Get user name and user ID of the above system user id is greater than 1 less user 500
awk -F: '{if($3>=1&&<=500){print $1,$3}}' /etc/passwd
Two, awk works
1, in order to match the line, awk use tools for editing
2, a whole line of $ 0, $ 1 for the first ..., $ NF represents the last
3, pattern; pattern matching a corresponding field line filter;
4, the corresponding command output formats do printf
Three, awk usage
awk [option] … 'program' FILE
note:
1, program must use single quotes
2, a number of program statements use braces contain up to be tied, nesting
awk ' {} Print ' / etc / the passwd # default print $ 0
Four, awk common option (option)
-F delimiter specified, the default is a space
-F [: \] specify multiple delimiters
Example: awk -F [: \] ' {$ Print. 3,. 5 $, $}. 7 ' / etc / the passwd specify: and / delimiters
-v manually specify variable parameters
awk -v a="a/b" 'print a' a.txt
1, a custom variable
2, call awk variables do not add the $ sign
The difference between the cut and awk
When a space is divided awk domain, is a single or multiple consecutive spaces as separator;
cut is based on a single space as a delimiter.
Five, awk's syntax ---- program
1、print
The default output (on the screen)
No Save command in awk, we can associate other commands (tee) to save
2, printf the formatting output
Output format: printf " xxx xxx% -10s% -10D " , name, NUM
Formatter:
% S display string
% D% i Display Value
% C ASCII display
% E% E calculated SCIENCES
% F shown float
% U unsigned integer
%% escape character, show only their own%
Modifiers:
The default is right-aligned
- on behalf of Left
% 5.4f deputies accounted for a median of 5, there are four decimal places
3, variable
Built-in variables ---- environmental variables (bash)
awk language supported by default variable
FS input variables defined delimiters
OFS defining output variables separator
The last one of the variables after NF ($ NF) split time variable references do not add $
The number of rows NR definition file, define multiple file, line numbers superimposed
FNR defining the number of lines in the file, counting only its own line number
FILENAME storage file name
BEGIN command is executed only once behind
awk 'BEGIN{print "xxx"}{print$3}' /etc/passwd
ARGC whole number of segments command parameter is not included ARGC command itself
ARGV calling command for the specified segment
awk '{print ARGC}' /etc/passwd == 2 awk '{print ARGV[2]}' /etc/passwd == /etc/passwd
RS specify line breaks, the default is \ n, you can specify a new line breaks, does not affect the default symbol
Replacing the default output ORS newline
awk -v ORS="@" '{print}' /etc/passwd
Custom Variables
-v variable = value
Behind the 'program', calls the custom variables, or can be used directly "variable value =" statement written program can
4, the pattern matching (address delimited)
a) a null value, not defined, it will be placed in the file, all the rows are circulated awk
b) for m ~ n lines operating
awk ' NR> = && NR. 1 <=. 3} {Print ' / etc / the passwd print line 1-3
c) pattern matching line / pattern /
awk '/r..t/{print}' /etc/passwd
d) / pattern1 /, / pattern2 / pattern1 first line to the first match pattern2
awk '/root/,/user1/{print}' /etc/passwd
Exercise: judge / patern1 /, the line between user / pattern2 / bash is the user, and displays the user name
awk -F: '/^root/,/user1/{if($NF=="/bin/bash");print $1,$3}' /etc/passwd
e) may be used as pattern matching judgment statement
awk -F: '$NF=="/bin/bash"{print $1 $3}' /etc/passwd
f) BEGIN definition statement before the operation to be executed in the default cycle
awk -F: ' the BEGIN {the printf "the shell program is the bash: \ n-"} $ of NF == "/ bin / the bash". 1 {Print $, $. 3} ' / etc / the passwd
g) END defines the end of the cycle is executed after
awk -F: $NF=="/bin/bash"{print $1,$3} 'END{printf "end\n"}' /etc/passwd
5, the operator
Operator that
+ - * /% ^ (power) //
Comparison Operators
> <==! => = <~ = (Equal to)! ~
awk -F: '-FS~"/bin/bash" {print $1,$3}' /etc/passwd
Logical Operators
&& || !
Assignment operator
= += -= /= *= %= ^= //=
Conditional expression
? Conditional statement statement condition is true; condition is not met statement
awk ' / ^ title / {of NF <Print = 2;? Print "Too few parameters"} ' /boot/grub/grub.conf
6, common action
print printf and manipulate it any command is action
a) expressions
b) input statements input statement
c) combining statement compound statements / pat1 / {{} {}}
d) control statements controlling, for example, if while statements like
e) output statements output statements
7, common language
a) if statement
Syntax: if (conditional expression) {} else {execute the statement to execute the statement}
awk '/^title/{if(NF<=2){print} else {print "error"}}' /boot/grub/grub.conf
b) while statement
Only when the line parameters to traverse only use the while statement
Syntax: Initial value while (conditional expression) {loop body; initial control statements}
c) for statements
Syntax: for (initial value; Analyzing condition; control statements initial value) {} loop
d) do-while statement
Syntax: do {loop} while (loop condition)
e) out of loop
BREAK [n] out n cycles
continue to jump out of this cycle
The default next out of the current cycle NR% 2 == 1 next odd rows skipped
awk '{if(NR%2==1){next}else{print}}' /etc/passwd
f) switch statement (similar to the case)
Syntax: switch (expression) {case pattern matching value: execute a statement; case ..., default: execute statement}
8, array
In awk, the same array and array characteristics of the shell
Note: awk Array not defined, just use, there is an empty array of default
Line traverse: traverse the entire file
Traversing column: a column taken to traverse the object
Note: Array by for statements give other variable assignment, the index information is assigned
Exercise : the number of statistical / etc / fstab each word (separated by a space word) of
awk -v RS=" " '{print}' /etc/fstab | awk '{a[$1]++}END{for(i in a){printf "%-50s = %-2d\n",i,a[i]}}'
9, function
Built-in functions:
length () statistics string length
Function sin () cos () tan () using mathematically ...
sub (x, x, x) to replace the first value matches
awk -F: ' {Print Sub (o, O, $. 1)} ' / etc / o passw a first column replaced with O
gsub (x, x, x) for all values matched to replace all the row
awk -F: ' {Print gsub (o, O, $. 1)} ' / etc / o all the passwd first column replaced with O
split (x, x, x) designated to cut file delimiter
netstat -tan | awk '/^tcp\>/{split($5,ip,":");print ip[1]}'