One: awk is the report generator
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/47/7D/wKioL1P7IkWyOEMRAABpBl-b3n0767.jpg "title=" Awk.png "alt=" Wkiol1p7ikwyoemraabpbl-b3n0767.jpg "/>
650) this.width=650; "src=" http://slayer.blog.51cto.com/e/u261/themes/default/images/spacer.gif "style=" border : 1px solid RGB (221,221,221); Background-image:url ("http://slayer.blog.51cto.com/addblog.php"); background-position : 50% 50%;background-repeat:no-repeat; "alt=" Spacer.gif "/>
awk field: awk cuts the contents of the row into each field by delimiter on each line of the document.
$: Represents the entire row.
$ ... $N: represents each field separated by a separator.
II: awk BASIC syntax
Basic syntax:awk [Options] ' program ' File file ...
awk [options] ' pattern{action} ' file File ...
input option: -f Specifies the separator option.
awk - F: ' {print $, $7} '/etc/passwd #指定以冒号作为字段分割符.
Output of the Pattern:awk
Print item1, Item2, .....
Key points:
(1) Each item is separated by commas, and
the output is separated by an output delimiter, and
(2) Each item of the output can be a string or numeric value, a field of the current record, a variable, or an expression of awk, and the value is implicitly converted to a string after the output;
(3) Print after item if omitted, the equivalent of print to output the entire string of content; to output whitespace later, use pirnt "" (Output in awk
Substring to use double quotes);
awk variables: Built-in variables, custom variables
built-in variables: field separators at input (begin before awk's built-in variables)
fs:field seperator, field delimiter at input
# awk ' begin{fs= ': "}{print $1,$7} '/etc/passwd #内置变量的引用要用到双引号
Rs:record seperator, enter line delimiter
# awk ' begin{rs= ': "}{print} '/etc/passwd #会把冒号分开的内容当作每一行来处理
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/47/7B/wKiom1P7IdDx_pavAABDgJYeX6Q550.jpg "title=" 5f50a529cfb64be89a7d2e8e85dee243.jpg "alt=" Wkiom1p7iddx_pavaabdgjyex6q550.jpg "/>
ofs:output field seperator, Fields delimiter at output
# awk ' BEGIN{fs= ': " ;
Ofs= ":"}{print $1,$7} '/etc/passwd #语句与语句之间要用分号分割 (red section)
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/47/7B/wKiom1P7If3iX_noAADtjNk1aK4594.jpg "title=" B72fc678a2ef48238c70a64cf341ee90.jpg "alt=" Wkiom1p7if3ix_noaadtjnk1ak4594.jpg "/>
ors:outpput row seperator, line delimiter at output
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/47/7C/wKiom1P7IlSQ-DYUAAGnaredKsU331.jpg "title=" 3.jpg " alt= "Wkiom1p7ilsq-dyuaagnaredksu331.jpg"/>
nf:numbers of field, number of fields #取最后一个字段还可用 $NF
nr:numbers of Record, number of rows if followed by multiple files, then all files are counted together;
# awk ' begin{fs= ': "; ofs=": "}{print NR $1,$7} '/etc/passwd/etc/group
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/47/7C/wKiom1P7I5PwimubAABqlRewKGw993.jpg "title=" 5.jpg " alt= "Wkiom1p7i5pwimubaabqlrewkgw993.jpg"/>
FNR: Number of rows; count each file separately ;
# awk ' begin{fs= ': "; ofs=": "}{print FNR $1,$7} '/etc/passwd
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/47/7E/wKioL1P7JQ-iVyqVAAECcpFX6Hs234.jpg "title=" 6.jpg " alt= "Wkiol1p7jq-ivyqvaaeccpfx6hs234.jpg"/>
ARGV: An array that holds the character of the command itself, awk ' {print $} ' 1.txt 2.txt, meaning argv[0] saves awk,
# awk ' Begin{print argv[0], argv[1],argv[2],ARGC} '/etc/passwd/etc/group
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/47/7C/wKiom1P7JCSS0kiOAABo9Kv6z10251.jpg "title=" 7.jpg " alt= "Wkiom1p7jcss0kioaabo9kv6z10251.jpg"/>
ARGC: Save the number of arguments in the awk command
Custom variable: Field delimiter at input (use begin before Awk's built-in variable )
-v var_name=value variable name distinguishing character case
(1) variable can be defined in program;
(2) can customize variables via the-v option on the command line;
# awk ' begin{a= ' Hello awk '}{print A} '/etc/passwd
650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/47/ 7e/wkiol1p7jyyjqr0paacatkhyeyc791.jpg "title=" 8.jpg "alt=" wkiol1p7jyyjqr0paacatkhyeyc791.jpg "/>
#awk ' begin{a= ' Hello awk ';p rint A} ' #可以不跟文件 output Hello awk directly
#awk-v a= "Hello awk" ' Begin{print A} ' #直接输出hello awk
Awk's printf command
Use format for commands: printf format, item1, item2,...
Points:
(1) to specify format;
(2) will not be automatically wrapped, if you want to change the line, you need to give \ n
(3) format is used to specify its output format for each subsequent item;
The format indicator is preceded by%, followed by a character:
%c: The ASCII code that displays the characters;
%d,%i: decimal integer;
%e,%e: The scientific counting method shows the numerical value;
%f: Displays floating-point numbers;
%g,%g: Displays values in scientific notation format or floating-point number format;
%s: Display string;
%u: Displays unsigned integers;
Percent: show% itself;
Modifier:
#: Display width
-: Align Left
+: Display symbols for numeric values
. #: Value Accuracy
# awk-f: ' {printf '%15s%-15s\n ', $1,$7} '/etc/passwd
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/47/7E/wKioL1P7Jezx7DtvAAFFWrKT3JI478.jpg "title=" 9.jpg " alt= "Wkiol1p7jezx7dtvaaffwrkt3ji478.jpg"/>
# awk ' BEGIN {printf '%f\n ', 3.1415} ' #显示7个字节
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/47/7E/wKioL1P7JhnB-qtZAAA8c1BBAgg700.jpg "title=" 10.jpg "alt=" Wkiol1p7jhnb-qtzaaa8c1bbagg700.jpg "/>
# awk ' BEGIN {printf '%f\n ', 3.1415926} ' #总位数超过8位, rounding after decimal point
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/47/7C/wKiom1P7JSvj4lC4AABBn1wV8T4765.jpg "title=" 11.jpg "alt=" Wkiom1p7jsvj4lc4aabbn1wv8t4765.jpg "/>
# awk ' BEGIN {printf '%15.2f\n ', 3.1415926} ' #15.2 indicates that the whole is 15 bits wide plus two digits after the decimal point
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/47/7E/wKioL1P7JnXSLJPKAABFFslokO8387.jpg "title=" 12.jpg "alt=" Wkiol1p7jnxsljpkaabffsloko8387.jpg "/>
awk output Redirection
Print Items > Output-file
Print Items >> output-file
Print Items | Command
Special File Descriptor:
/dev/stdin: Standard input
/dev/stdout: Standard Output
/dev/stderr: Error Output
Awk's operator
Arithmetic operators:
X+y
X-y
X*y
X/Y
X**y, X^y
X%y
-X: Negative value
+x: Convert to Numeric
String operators: Connecting
Assignment operators:
=
+=
-=
*=
/=
%=
^=
**=
++
--
If the pattern itself is the = sign, write as/=/
Comparison operators:
<
<=
>
>=
==
!=
~: Pattern matching, the left string can be matched to the right pattern to true, otherwise false;
!~:
Logical operators:
&&: With
|| : OR
Conditional expression:
Selector?if-true-expression:if-false-expression
# awk-f: ' {$3>=500?utype= ' common user ': utype= ' admin or System user ';p rint $, ' is ', utype} '/etc/passwd
Function call:
Function_name (ARGU1,ARGU2)
This article is from the "Slayer" blog, make sure to keep this source http://slayer.blog.51cto.com/4845839/1544885
The awk of Linux