1, awk introduced [alone] programming language interpreter first letter of the full name Aho Weinberger Kernaighan three abbreviation; 1970 for the first time on a Unix machine, then use it in the open field; so we use in Linux, renamed as GNU awk; so, in fact, Linux is called gawk; grep line filter parameters: -o display only matches -v -i ignore case negated -E extended regular expressions sed line editor parameters: -n cancel the default output p Print use are: address delimited, the regular expression command: p a I W C S awk report generator by pattern matching and their own language formats, to obtain, and outputs the contents of the customer needs; awk -F: '{IF ($ 3> = 1 && $ 3 <= 500) {print $ 3}} '/ etc / passwd time to achieve this function # awk last word can only need! meaning formatted output:
awk -F: "." -V OFS = 'BEGIN {printf "username udi \ n =================== \ n" {if ($ 3> = 18 & $ 3 <= 500) {printf "user name:% - 10s UID:% - 10d \ n", $ 1, $ 3} END {printf "\ ------------------- ----------- \ Nend \ n-"} '/ etc / Pass
2, awk works
sequentially matched to the line using tools for editing or formatting awk printout, the loop on the line, in awk, can not use
. 3, awk usage
awk parameters ... '' .. the FILE
. 1) program must use single quotes
2) a plurality of program statements comprising braces together, side by side can be nested
awk 'Print' / etc / the passwd
. 4, the common parameters awk
-F specified delimiters may also specify multiple -F awk [/:]
-v because awk is a language compilers, are able to define their own variables colleagues have built-in variables (similar to the environment variables)
and -v is to manually specify the parameters
awk -va = "a / b" '{print a}' a.txt assigned to a print a variable
1) a may custom variable -v FS " : "
2) do not add to call custom variable $ symbol in awk
awk '{a = "a / b"; print a}' a.txt
5, awk syntax of --program
1) Print
default output (displayed on the screen)
is not saved in awk command, we can associate another command to save the results
2) printf-- achieve formatted output
printf "% s is % d best student class "variable 1, variable 2
specifier:
% S
% d value I%
% e% E number of scientific computing
% c Acs values,
% F float
%% escape character, show only their own%
% u unsigned integer
modifiers:
default is right aligned
- left aligned Representative
% 5.4f 5 represents the decimal proportion taken bits 4
3) variables (built-in variables, custom variable)
built-in variables - environmental variables (bash) viewing environment variable command (env, set -C + C)
variable awk language supported by default
variable delimiter FS defining input
variables defining output OFS separator
NF defines the number of parameters after division (partition $ NF after a final variable )
The number of lines NR definition file, a file defining a plurality of overlay files line number
FNR document count only their row number
awk Print {NR} '/ etc / fstab / etc / the passwd
awk' {FNR} Print '/ etc / fstab / etc / the passwd
fILENAME storage file name
awk '{print fILENAME}' / etc / passwd // N times to print the file name, the file number of rows N
awk 'BEGIN {print fILENAME}' / etc / passwd // BEGIN of means to perform only once the print
number of stages ARGC entire command is [not contain program itself]
awk 'the BEGIN {Print ARGC} / etc / the passwd / etc / fstab / etc / Shadow
ARGV array to the transfer of command, specified segment e.g. ARGV [2]
the RS specify line breaks can specify line breaks the heart, itself, does not affect the line breaks
when a specified output newline ORS, replace the default newline newline specified
custom variable
-v variable = value
at the back 'program ', calls the custom variable 4, can be used directly
or "variable = value" written directly'program 'which
6, pattern matching (address delimited )
1) null value, not defined, all rows will document, circulated into awk
2) of a fixed line 3 to operate
awk '1,3 {print } '# 1-3 Note 3 awk not supported by default like
awk' NR> = && NR. 1 <=. 3; Print {} '/ etc / the passwd
. 3) matching
awk' /r..ter/{print} '/ etc / the passwd
. 4) the matching pattern may be used as decision statement
awk -F: '$ of NF == "/ bin / the bash" {Print $. 1, $. 3}' / etc / the passwd
. 5) BIGEIN | the END statement
BIGEIN defined in the default cycle statement before the operation to be performed
awk -F: 'BEGIN {printf " shell program for the bash user to: \ n-"} $ of NF == "/ bin / bash" {Print $. 1, $. 3}' / etc / the passwd
awk - F: 'BEGIN {printf "shell bash procedure for the user to: \ n"} $ NF = = "/ bin / bash" {print $ 1,$ 3} END {printf "end \ n"} '
at the end of time generally in the formatted output, print header and;
7, operator
Arithmetic operators:
+ - * / **% @
comparison operators:
==! => <> = <=
~! ~
Awk -F: '$ of NF ~ "/ bin / the bash" {Print $. 1, $. 3}' / etc / the passwd
awk -F: '! $ Of NF ~ "/ bin / the bash" {Print $. 1, $. 3}' / etc / passwd
logical operators:
&&
||
!
Assignment operator:
= + = - = / = * =
conditional expression:
conditional statements? Conditions are met statement
8, Common Action
Print printf as well as any other operations are Action
9, common language
1) if statement
syntax: if (conditional expression) {execute the statement}
IF (conditional expression) {} else {execute the statement execution statement}
awk '/ ^ title / {IF (of NF <= 2) the else {} {Print Print parameters too small}}' /boot/grub/grub.conf
awk '/ title / {IF (of NF <= 2). 4 Print $}' /boot/grub/grub.conf
2) the while statement is
only when the parameters of the line will be used to traverse the loop;
syntax: while (conditional expression ) {loop body}
awk / ^ [[: spacel] * Kernel \> / {I =. 1; the while (I <= of NF) {the printf $ I "\ T"; Print length ($ I); I + +}} 'the grub.conf
. 3) for statement
syntax: for (initial value; conditional control statements initial value) {loop body}
awk' / ^ [[: Space:] * Kernel \> / {for (I =. 1; I <= NF; i ++) { printf $ i "\ t"; print length ($)}} grub.conf
cycle is recommended for statement
exercise: ah statistics about the number of times a file specified row word appears;
in awk , for shell may be used in the same format as:
review:
for I in the list; do
loop
DONE
for (I in list) {loop body}
echo "xia Liang Z Shi Shuai K n-HEN Le DA FA"| awk
4) do-the while loop
Syntax: do {loop} while (loop condition)
difference and while the attention: while statement only when the condition is satisfied, will enter the circulation, and do while will first perform a loop, and then will be evaluated
5) out of circulation statement
break [n] out of the current cycle;
Continue out of the current cycle;
Next out of default the current cycle;
awk '{IF (== NR 2%. 1) {Next the else {}}} Print' / etc / the passwd
. 6) switch case similar
syntax:
switch (expression) {case pattern matching value; execute statement; case pattern matching value; executing the statement. . . , Default; execute the statement}
10, arrays
in awk, the same array and an array of characteristics of the shell;
Note: awk Array not defined, as long as the use, there is an empty array of default; do this in the time statistics very common
line traversing ---> actually traverse the entire file
column traverse ---> take an object fixed in a column, the same statistical data
Note: array by a for statement, to give time to other variable assignment, the assignment the index is index information
practice: statistics about the number of times a file specified row word appears;
awk -v RS = "" {print } a.txt | awk "{ls [$ 1] + +} END {for (i in ls) {print ls [[i]}} '
Exercise: Statistics - next file the number of each word (character string separated by a space) occur;
awk '{for (I =. 1; I <= of NF; I + +) {COUNT [$ I] + + {}} for the END (COUNT I in ) {Print I, COUNT [I]} '/ etc / fstab
awk' {for (I =. 1; I <= of NF; + +) {LS [$ I] + +} the END {for (J in Is) { J Print, LS [J]} " '/ var / log / the httpd / Access log | -t Sort" "-NR -k2
. 11, function
1) built-in function
calls the function: funcation (parameter)
length () statistics string the length of the
line used on the number of operations of mathematical sin ()
Sub (X, X, X) to replace the first matching value
awk '{print sub (o, O, $ 1)}' / etc / passwd // replace the o the first column O file (a match)
gsub
awk '{Print gsub (o, O, $. 1)}' / etc / o to replace the passwd column O of the first file (match all)
Split (X, x,x) cut to the specified file delimiter
netstat -tan | awk '/^tcp\> /{split($5,ip,":");prin ip[1]}'
netstat -tan | awk '/^tcp\> /{split($5,ip,":");counti[ip[1]]+ +}END{for (i in count) {print i,count[i]}}'
2)自定义变量