[awk separate programming language interpreter]
1, awk Introduction
Full name: Aho Weinberger Kernaighan three men acronym;
In 1970 for the first time on a Unix machine, then use it in the open field;
So, we use in linux, renamed GNU awk; in fact, called gawk linux
gcc --- c language interpreter
RSA
Grep line filter
-o -i -v -e egrep fgrep grep
Sed line editor / editor stream
The operation mode of each row are put into the space
-n p --- Print
Address delimitation 3,5 command pai \ \ wc \ s / pattern / string / g | 1 | 2 ... | &
Awk report generator [By default, three tools are not edit the source file]
By pattern matching language format as well as themselves, to get and print the contents of the customer needs;
Examples: Get user id, the above system is equal to 500 greater than or equal to a user's user name and user ID
a=`awk -F: '{print $3}' /etc/passwd`
The last time awk to achieve this function needs only one sentence
2, awk principle
Belongs to the line of treatment, do not have to cycle
Sed with the same
The extracted single line, separated by spaces by default
$0 $1 $2
The first step in doing split
The second step, to match, pattern, pattern matches the corresponding fields in the filtered lines (address delimited)
A third step, through a corresponding command output format do -printf
[Difficulty: association logical statement, he has his own syntax]
Sequentially matched to the line using tools to edit or awk formatted output
[So, about the circulation line, in awk, you can not use]
awk -v FS=":" '{if($3>=1&&$3<=500){print $3}}' /etc/passwd
3, awk usage
awk [option]…‘program’ FILE…
program must use single quotes
Multiple program statements use braces contain up to be side by side, you can nest
awk '{print}' / etc / passwd ----- 0 $ with a default
4、option
-F specify the delimiter can specify multiple delimiters awk -F [/:] '{print $ 1, $ 2, $ 3}' q.txt
-v because awk is a language compiler, can define their own variables, but also have built-in variables (similar to environment variables)
Manually specify variable parameters
Assignment to a print a variable
awk -v a="a/b" '{print a}' q.txt
It is a custom variable -v FS = ":"
Call in awk variables do not add the $ sign
Understand the difference between the cut and awk
5, awk their fixed grammatical sentence
a) print the default output (print on screen at once) no Save command in awk, you can be associated with other commands to save
[root@localhost ~]# awk '{a="a/b" ; print a}' q.txt | tee q.bak
a/b
[root@localhost ~]# cat q.bak
a/b
b) printf
Display formatted output
printf "% s is% c% d best student in the class to learn", $ a, $ b, $ c
[Relationship] with only the order
Formatter
% S ------------- string
% D% i -------- value
% E% E -------- SCIENCES calculated values
% C ------------ ACSII code value
%%% ------------ escape character only display their
% F ------------- float
% U ------------ unsigned integer
Modifiers
The default is right-aligned
- on behalf of Left
+ Represents the right-aligned
% 5.4f 5 cent representation representing the taken digits 4 decimal places
awk '/ ^ UUID / {printf "is mounted File:% -10s mount point:% - 10s file system formats:% - 10s \ n", $ 1, $ 2, $ 3}' / etc / fstab
Is mounted File: UUID = b35bca91-5dc3-4539-beb2-50f7d26389ff mount point: / boot file system format: xfs
Note Address delimitation sed / pattern1 /, / pattern2 /
c) Variable (built-in variables, custom variable)
Built-in variables - environmental variables (bash) (env, set -C + C)
Awk language supported by default variable
FS input variables defined delimiters
OFS defining output variables separator
NF defines the number of parameters is later divided (divided $ NF after a final variable)
* When variable references, do not add $ $ 0, $ 1 $ n
awk -v FS=":" '/\/bash$/{print $NF}' /etc/passwd
awk -v FS=":" '/\/bash$/{print $1,$NF}' /etc/passwd
When the file number of lines to define multiple file NR-defined line numbers superimposed
awk ‘{print NR}’/etc/passwd /etc/fstab
FNR file count only their own line number
awk ‘{print NR}’/etc/passwd /etc/fstab
FILENAME storage file name
awk '{print FILENAME}' / etc / passwd - The file name is printed N times, N being the number of rows of the document, printed only once using the BEGIN
awk 'BEGIN {print FILENAME}' / etc / passwd // BEGIN {----} statement is executed only once at the beginning of the cycle
ARGC number of stages of the entire command, [does not contain the command itself]
cut -d: -f1 /etc/passwd ----- 4段
awk 'BEGIN{print ARGC}' /etc/passwd /etc/fstab /etc/shadow
ARGV array for calling command, a designated section ARGV [2]
* Note * array does not contain program
RS specify the newline character \ n you can specify a new line breaks, line breaks does not affect the itself
awk -v RS=" " '{print}' /etc/passwd
ORS output when a specified line breaks, replace the default newline character is specified
awk -v ORS = "@" '{print}' / etc / passwd // cancel wrap
Custom Variable -v variable = value
When the rear 'program', calls the custom variables can be used directly
Or "variable value =" statement written directly in 'program' is also