awk [single programming language interpreter]
1, awk introduce
full name: Aho Weinberger Kernaighan three men acronym;
1, awk introduce
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; so, in fact, Linux is called gawk;
so we use in Linux, renamed GNU awk; so, in fact, Linux is called gawk;
grep line filter
-o -i -v -E grep egrep fgrep write anything to match what []
sed line editor
-np 1, address delimited 3,5, / pat / e [2], the command pa \ i \ WC \ S / PAT / STR / G |. 1 | 2 .. | &
$ {var / PAT / STR $} var {// PAT / STR}
$ {} Test, Test # $ {}, {$ Test: offset : length}, $ {test # * word}, $ {test ## * word}, $ {test% word}, $ {test %% * word}, $ {test / pattern / string}, $ {test / / pattern / String}, {$ Test: -word}
awk report generator
by pattern matching and language format itself, to obtain, and outputs the required contents of the client;
[default, not three tools to edit Source File】
-o -i -v -E grep egrep fgrep write anything to match what []
sed line editor
-np 1, address delimited 3,5, / pat / e [2], the command pa \ i \ WC \ S / PAT / STR / G |. 1 | 2 .. | &
$ {var / PAT / STR $} var {// PAT / STR}
$ {} Test, Test # $ {}, {$ Test: offset : length}, $ {test # * word}, $ {test ## * word}, $ {test% word}, $ {test %% * word}, $ {test / pattern / string}, $ {test / / pattern / String}, {$ Test: -word}
awk report generator
by pattern matching and language format itself, to obtain, and outputs the required contents of the client;
[default, not three tools to edit Source File】
Examples: Get user id above system is equal to 1 less than the user 500 user name and user ID
for I in $ (Cut -d: - f3 / etc / the passwd); do
IF [I $ I $ -ge 1 -a - 500 Le]; the then
echo grep $ I | Cut -d: -f1,3
Fi
DONE
awk -F: '{IF ($. 3> = $. 3. 1 && <= 500) {}}. 3 Print $' / etc / the passwd
# final awk to achieve this function only when you need a word on it!
for I in $ (Cut -d: - f3 / etc / the passwd); do
IF [I $ I $ -ge 1 -a - 500 Le]; the then
echo grep $ I | Cut -d: -f1,3
Fi
DONE
awk -F: '{IF ($. 3> = $. 3. 1 && <= 500) {}}. 3 Print $' / etc / the passwd
# final awk to achieve this function only when you need a word on it!
Significance formatted output:
awk -F: -v the OFS = ":" 'the BEGIN {the printf "username UID \ n-======================== \ n "} {if ($ 3> = 1 && $ 3 <= 500) {printf" user name:% - 10s UID:% - 10d \ n ", $ 1, $ 3}} END {printf" ------- -------------------- \ nend \ n "} ' / etc / passwd
awk -F: -v the OFS = ":" 'the BEGIN {the printf "username UID \ n-======================== \ n "} {if ($ 3> = 1 && $ 3 <= 500) {printf" user name:% - 10s UID:% - 10d \ n ", $ 1, $ 3}} END {printf" ------- -------------------- \ nend \ n "} ' / etc / passwd
2, awk works
xxxxx (Figure)
xxxxx (Figure)
3, awk usage
awk [the Option] ... 'Program' FILE ...
1, Program must be used! apostrophe!
2, a number of program statements use braces contain up to be side by side, you can nest
awk [the Option] ... 'Program' FILE ...
1, Program must be used! apostrophe!
2, a number of program statements use braces contain up to be side by side, you can nest
awk '{print}' /etc/passwd
4, the common Option awk
-F specified delimiter
awk -F [/:] '{ print $ 1, $ 3}' a.txt
which represents any one of a plurality of characters in []
-v because awk is a compiled language , a variable can define their own, but also has built-in variables (environment variables and the like)
manually specify variable parameters
awk -va = "a / b" '{print a}' a.txt
to assign a print a variable
1, a A custom variable is the FS = -v ":"
2, call in awk variable call without adding $ symbol
awk '{a = "a / b"; print a}' a.txt
-F specified delimiter
awk -F [/:] '{ print $ 1, $ 3}' a.txt
which represents any one of a plurality of characters in []
-v because awk is a compiled language , a variable can define their own, but also has built-in variables (environment variables and the like)
manually specify variable parameters
awk -va = "a / b" '{print a}' a.txt
to assign a print a variable
1, a A custom variable is the FS = -v ":"
2, call in awk variable call without adding $ symbol
awk '{a = "a / b"; print a}' a.txt
Extended: understand the difference between the cut and awk;
. 5, the syntax awk - Program
. 1, Print
default output (on the screen)
is not stored in the command awk, we can associate another command to save the results of awk;
awk 'A = { "A / B"; Print } a 'a.txt | TEE a.bak
2, the printf - the formatting output
printf "% s% d is the best class students learn" variable 1, variable 2
[] with only sequential relationship
specifier
% string s
% d% i value
% e% E scientific calculation value
% c ACSii code value
% f float
% u unsigned integer
%%% show only their escape character
modifier
default is right-aligned
- left aligned Representative
% 5.4F
5-digit percentage
four decimal places taken
awk '/ ^ UUID / {printf " is mounted file:% - 50s mount point:% - 10s file system formats:% - 10s \ n", $ 1, $ 2, $ 3 } '/ etc / fstab
Note: the address mentioned here delimited
sed / PAT1 /, / PAT2 /
3, variables (built-in variables, custom variable)
built-in variables - environmental variables (bash) (env, set -C + C)
variable awk language supported by default
FS separator defining input variables
OFS separator defining output variables
the number (a variable after the last partition $ NF) NF parameters after the partition definition line
* time variable references, do not add $, $ 0, $ ... $. 1 n-
awk -v the FS = ":" '/ \ / the bash $ / {Print $. 1, $ of NF} '/ etc / the passwd
NR definitions file number of rows, defining a plurality of files, the line number is superimposed
FNR document count only their row number
awk' {print NR} '/ etc / fstab / etc / the passwd
awk '{Print the FNR}' / etc / fstab / etc / the passwd
name of the file is stored fILENAME
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 { statement} row beginning of the cycle only performed once;
the number of stages of the entire command ARGC [Note: do not contain 'program' Itself]
ARGV array for calling command, a designated section ARGV [2] [Note: the array nor the 'program']
awk 'Print ARGC the BEGIN {}' / etc / the passwd / etc / fstab / etc / Shadow
awk 'Print ARGV the BEGIN {[. 3]}' / etc / the passwd / etc / fstab / etc / Shadow
the RS specified newline \ n can specify a new line breaks, line breaks itself does not affect the
ORS output when the specified line breaks, line breaks replace the default specified character
awk -v the RS = "" 'Print {}' / etc / the passwd
awk ORS = -v " " '{print}' / etc / passwd // wrap may be used to cancel the
custom variable
-v variable = value
when the rear 'program', calls the custom variables can be directly used
or" variable value = "statement written directly 'program' can;
awk -VA = "A / B" 'Print {A}' a.txt
awk 'A = { "A / B";} A Print' a.txt
. 1, Print
default output (on the screen)
is not stored in the command awk, we can associate another command to save the results of awk;
awk 'A = { "A / B"; Print } a 'a.txt | TEE a.bak
2, the printf - the formatting output
printf "% s% d is the best class students learn" variable 1, variable 2
[] with only sequential relationship
specifier
% string s
% d% i value
% e% E scientific calculation value
% c ACSii code value
% f float
% u unsigned integer
%%% show only their escape character
modifier
default is right-aligned
- left aligned Representative
% 5.4F
5-digit percentage
four decimal places taken
awk '/ ^ UUID / {printf " is mounted file:% - 50s mount point:% - 10s file system formats:% - 10s \ n", $ 1, $ 2, $ 3 } '/ etc / fstab
Note: the address mentioned here delimited
sed / PAT1 /, / PAT2 /
3, variables (built-in variables, custom variable)
built-in variables - environmental variables (bash) (env, set -C + C)
variable awk language supported by default
FS separator defining input variables
OFS separator defining output variables
the number (a variable after the last partition $ NF) NF parameters after the partition definition line
* time variable references, do not add $, $ 0, $ ... $. 1 n-
awk -v the FS = ":" '/ \ / the bash $ / {Print $. 1, $ of NF} '/ etc / the passwd
NR definitions file number of rows, defining a plurality of files, the line number is superimposed
FNR document count only their row number
awk' {print NR} '/ etc / fstab / etc / the passwd
awk '{Print the FNR}' / etc / fstab / etc / the passwd
name of the file is stored fILENAME
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 { statement} row beginning of the cycle only performed once;
the number of stages of the entire command ARGC [Note: do not contain 'program' Itself]
ARGV array for calling command, a designated section ARGV [2] [Note: the array nor the 'program']
awk 'Print ARGC the BEGIN {}' / etc / the passwd / etc / fstab / etc / Shadow
awk 'Print ARGV the BEGIN {[. 3]}' / etc / the passwd / etc / fstab / etc / Shadow
the RS specified newline \ n can specify a new line breaks, line breaks itself does not affect the
ORS output when the specified line breaks, line breaks replace the default specified character
awk -v the RS = "" 'Print {}' / etc / the passwd
awk ORS = -v " " '{print}' / etc / passwd // wrap may be used to cancel the
custom variable
-v variable = value
when the rear 'program', calls the custom variables can be directly used
or" variable value = "statement written directly 'program' can;
awk -VA = "A / B" 'Print {A}' a.txt
awk 'A = { "A / B";} A Print' a.txt