Shell script (vi)
(2) awk
A, awk programming mode
#awk编程模式分三个阶段: Execute code Snippet before reading input file (identified by the BEGIN keyword)
Execute code snippet after #读取输入文件时执行代码段, reading input file (identified by end keyword)
B, pattern matching
#awk语句由模式 (pattern) and action composition (action).
#匹配空白行
awk '/^$/{print ' a blank line '} ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/47/wKiom1V34ZDBz4-tAABo7J-EHt4758.jpg "/>
c, records, and domains
#awk将每个输入文件定义为记录, each string in the row is defined as a field, with spaces between fields, tab
#键或其他符号进行分隔, the symbol for a delimited field is called a delimiter. $ is the domain operator, performing actions on the specified domain
The #$1 represents the 1th field, the $ $ = 2nd field, the $ $ represents the 3rd field, and so on, and the $
#如文件num. txt content, delimited by ":", is divided into 4 domains: num, 123, name1, and Test1
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/43/wKioL1V34zbhgywvAABw1lNZUTI250.jpg "/>
#分隔符也可以使用BEGIN标识中的FS变量来改变
awk ' BEGIN {fs= ': '} {print $ ', ' $ num.txt} '
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/47/wKiom1V34ZHjh7AxAABKS_MWNFY545.jpg "/>
d, Relationships, and Boolean operators
#关系运行符 <, <=, >, >=, = =,! =, ~,!~: Less than, less than or equal to, greater than, greater than or equal to
#等于, not equals, matches regular expressions, does not match regular expressions
awk ' BEGIN {fs= ': '} {if ($3<1) print '} '/etc/passwd
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/43/wKioL1V34zaQZ04uAABDCIOMP8Q052.jpg "/>
#匹配正则表达式和不匹配正则表达式
awk ' BEGIN {fs= ': '} $2~/123/' Num.txt
awk ' BEGIN {fs= ': '} $2!~/123/' Num.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/47/wKiom1V34ZGip_9DAACgHfjg1bo094.jpg "/>
#布尔运算符 | |, && and!: respectively, for or, with, non-
awk ' BEGIN {fs= ': '} {if ($2==123 | | $4== "Test2") print $} ' Num.txt
awk ' BEGIN {fs= ': '} {if ($1== "num" && $2==122) print $} ' Num.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/47/wKiom1V34ZKDkRnNAACgz3VpY6M072.jpg "/>
#算术运算 + 、-、 *,/,%, ^ or * *, ++x, x + + 、--x and x--: Add, subtract, multiply, divide, modulo,
#返回x值前自增1和返回x值之后自增1, returns the X minus 1 and the 1 after the X value is returned
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/43/wKioL1V34zeShUVIAADI4vVOWdk759.jpg "/>
Awk-f: '/num/{print $5+$2} ' num.txt
Awk-f: '/num/{print $5-$2} ' num.txt
Awk-f: '/num/{print $5*$2} ' num.txt
Awk-f: '/num/{print $5/$2} ' num.txt
Awk-f: '/num/{print $5%$2} ' num.txt
Awk-f: '/num/{print $2^2} ' num.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/47/wKiom1V34ZLg10hpAAGGhXd_4wo875.jpg "/>
e, System variables
# $n: Nth field of the current record, divided by FS between domains
#$0: The domain of the record
#ARGC: Number of command-line arguments
#ARGING: The location of the current file in the command line (identified as 0)
#ARGV: Array of command-line arguments
#CONVFMT: Digital Conversion format
#ENVIRON环境变量关联数组
#ERRNO: Description of the last system error
#FIELDWIDTHS: List of field widths, separated by spaces
#FILENAME: Current file name
#FNR: The number of records in a file, that is, how many records (lines) are in the file
#FS: Specify the domain delimiter, which is the default space
#IGNORECASE: Boolean variable, if straight, ignores case matching
#NF: The number of fields in the current record, that is, the current record, the number of fields separated by delimiters
#NR: Current record number, which is currently read to the first record
#OFMT: The output format of a number
#OFS: Output field delimiter, default to Space
#ORS: Output record delimiter, default is line feed
#RLENGTH: The length of the string matched by the match function
#RS: Record delimiter, default to Space
#RSTART: The 1th position of a string matched by the match function
#SUBSEP: Array subscript delimiter, default value is \034
#以 ":" is a delimiter that prints the number of fields in the current record, the number of records, all fields, and the number of file names and records
Awkl ' BEGIN {fs= ': '} {print nf,nr,$0} END {print Filename,fnr} ' Bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/43/wKioL1V34zjwxzFOAACFppOQdvU311.jpg "/>
F, formatted output
#printf (formatting controls, parameters), the controls start with%, and the parameters are generally domain
#修饰符 "-", "width", and ". Prec", respectively: left-justified, field-step, and multiples to the right of the decimal point
#格式符 "%c", "%d", "%e", "%f", "%o", "%s", and "%x", respectively: ASCII characters, number of shaping
#浮点数, scientific notation, floating point number, octal, string, and hexadecimal
#以 ":" As a delimiter, format the output num.txt 1th and 4th fields
Awk-f: ' {printf (%s\t%f\n,$1,$4)} ' Num.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/47/wKiom1V34ZPhoW6yAABNQ4EXclU563.jpg "/>
2 digits after #浮点数长度控制在5位, decimal point
awk ' BEGIN {printf ("%5.2f\n", 20150506.2101)} '
#小浮点数小数点保留3位, and Align Left
Awkl ' BEGIN {printf ("%-.3f\n", 20150506.2101)} '
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/43/wKioL1V34zihT4GRAACGtW-pGEY554.jpg "/>
g, built-in string functions
#gsub (r,s): replace R with s in the input file
#gsub (r,s,t): replace R with S in T
#index (S,T): Returns the 1th t position of a string in S
#length (s): return s length
#match (S,T): Tests if S contains a string that matches T
#split (r,s,t): R is divided into sequence s on t
#sub (R,S,T): Replaces the 1th occurrence of R in T with the S
#substr (R,s): Returns the suffix part of the string R starting from S, which intercepts the string from S start to end
#substr (R,S,T): Returns the suffix part of the string r from the beginning of the s length to T,
#即截取从s开始长度为t的字符串
#gsub ("name", "*"): Replace name with * in the input file
awk ' BEGIN {fs= ': '} gsub (/name/, ' * ') {print $} ' Num.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/47/wKiom1V34ZOTe9NNAABDq5Uorvo982.jpg "/>
#index ("Get_string_index", "string"): Returns the 1th "string" position, starting with 1
awk ' BEGIN {print index ("Get_string_index", "string")} '
#length ("Get_string_length"): Gets the string length
awk ' BEGIN {print length ("Get_string_length")} '
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/43/wKioL1V34zjiw097AACHc_VJobA368.jpg "/>
#match ("Test_string_exist", "string"): Tests whether string strings are contained in Test_string_exist
awk ' BEGIN {print match ("Test_string_exist", "string")} '
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/47/wKiom1V34ZOg4A_bAABVFc4ovtI322.jpg "/>
#sub ("Name2", "rep", $): Replace the 1th occurrence of name2 with rep
awk ' BEGIN {fs= ': '} $2~122 sub ("Name2", "Rep", $ $);p rint $} ' Num.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/43/wKioL1V34zngu6CrAABeP4Yu0mA719.jpg "/>
#substr ("Get_string_test", 5): Gets the string starting from 5 characters to the end of the line
#substr ("Get_string_test", 5,6): Gets a string starting from 5 characters with a length of 6 characters
awk ' BEGIN {print substr ("Get_string_test", 5)} '
awk ' BEGIN {print substr ("Get_string_test", 5,6)} '
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/47/wKiom1V34ZTzZeg_AACk6eSSXbk000.jpg "/>
H. Passing parameters to the awk script
I, conditional statements and loop statements
#if条件语句.
if (conditional expression)
{
Action
}
Else
{
Action
}
#for循环语句
for (n=0;n<=10;i++)
{
Action
}
#while循环语句, the 1th while executes at least 1 times,
#第2个while可能1次都没有执行 (conditions not met)
Do
{
Action
}
while (conditional expression)
while (conditional expression)
{
Action
}
This article is from the "Love On Action" blog, please be sure to keep this source http://1055745601.blog.51cto.com/5003160/1660417
Shell script (vi)