2、进行逻辑判断的表达式所返回的值有两种,如果逻辑值为true,则返回1,否则返回0。
3、语法中else 语句2以[ ] 前后括住表示该部分根据需要加入或省略。
while 指令
语法:while( 表达式 ) 语句
范例:
while( match(buffer,/[0-9]+/.c/ ) )
{ print “Find :” substr( buffer,RSTART,RLENGTH)
buff = substr( buffer,RSTART + RLENGTH) }
利用mid函数,返回文本串中从指定位置开始的特定数目的字符,该数目由用户指定。return并不是让函数马上返回,而是return语句执行后,将把返回结果放置进函数栈中,此时函数并不是马上返回,它要执行finally语句后才真正开始返回。你也可以定义多个条件,当条件被匹配时,就会去执行set指定的相关动作(set语句并非必须,例如:若route-map仅用于匹配感兴趣流量,则不需要set语句)。
do-while 指令
语法:
do 语句 while(表达式)
范例:
do{ print “Enter y or n ! ” getline data }
while( data !~ /^[YyNn]$/)
1、上例要求用户从键盘上输入一个字符,如果该字符不是Y,y,N或n,则会不停执行该循环,直到读取正确字符为YyNn为止。
do-while语句:先执行循环体,然后判断循环条件是否成立. 之后继续循环。执行过程:若条件成立,则执行循环体语句,执行后再判断条件是否成立,如果不成立则跳出循环。执行过程:表达式1(初值)----表达式2(判断初值是否满足条件)------循环体语句执行一次------表达式3(初值变化规律)------表达式2(判断值是否满足条件)------再执行循环语句,依次下去。
for循环语句
语法一:for(variable in array ) statement
范例:执行一下awk语句:
[root@myfreelinux pub]# awk ‘BEGIN{a["one"]=1;a[2]=2;a["three"]=3;for(one_array in a) printf(“a[%s]=%d/n”,one_array,a[one_array]);}’
a[three]=3
a[2]=2
a[one]=1
1、for循环可以查找数组中所有的下标值,并依次使用所指定的变量记录这些下标值,以本例而言,变量one_array将依次代表 “three”,“2”,“one”
2、for循环指令查找出的下标值之间没有任何次续关系。
语法二 :
nodes identified by expression1 are inserted as siblings directly before the node identified by expression2. the before keyword cannot be used when attributes are being inserted. for example, it cannot be used to insert an attribute constructor or to return an attribute from an xquery.。nodes identified by expression1 are inserted as direct descendents (child nodes) of the node identified by expression2. if the node in expression2 already has one or more child nodes, you must use either as first or as last to specify where you want the new node added. for example, at the start or at the end of the child list, respectively. the as first and as last keywords are ignored when attributes are inserted.。in the following example, an if condition is specified as part of expression1 in the insert xml dml statement. if the condition is true, an attribute is added to the
范例:for(i=1; i<=10; i++) sum=sum + i
说明:1、上例用来计算1加到10的总和。
2、expression1常用于设定该for 循环的起始条件,如 i=1;expression2 用于设定该循环的停止条件,比如i<=10;expression3 常用于改变expression1的值,直到使表达式2满足条件跳出循环,如i++。
break指令
break指令用以强迫中断(跳离) for,while,do-while 等循环。
范例:
while(getline < “datafile” > 0 )
{ if( $1 == 0 ) break
else print $2/$1 }
上例中,awk 不断地从数据文件datafile中读取资料,当$1等于0时,停止该执行循环。
continue 指令
循环中的运行一部分是,执行continue指令来跳过循环中尚未执行的语句。
范例:
for( index in X_array)
{ if( index !~ /[0-9]+/ )
continue
print “Juest print thedigitalindex”, index }
上例中如果index不是数字则执行continue,即跳过后面的print语句。continue和break 是不同的:continue只是跳过后面没有执行的语句,但不会离开该循环。
next 指令
执行next指令时,awk会跳过next指令之后的所有语句,包括next之后的所有Pattern { Actions },接着读取下一笔数据行,继续从第一个 Pattern {Actions} 执行起。
范例:
[root@myfreelinux pub]# cat integer
222 111
333 111
444 111
[root@myfreelinux pub]# cat action.awk
#!/bin/awk -f
{
if(/^[ /t]*$/)
{
print “this is a blank line,nothing here!”;
next;
}
$2!=0
{
printf(“$1 is %d,$2 is %d,$2/$1 is %d/n”,$1,$2,$1/$2);
}
}

[root@myfreelinux pub]# awk -f action.awk integer
$1 is 222,$2 is 111,$2/$1 is 2
$1 is 333,$2 is 111,$2/$1 is 3
this is a blank line,nothing here!
$1 is 444,$2 is 111,$2/$1 is 4
说明:awk 读入的数据行为空白行时( match /^[ /]*$/ ),除打印消息外只执行next,即awk将跳过其后的指令,继续读取下一行数据,从头开始并再次执行if(/^[ /t]*$/)语句
exit 指令
执行exit 指令时,awk将立刻跳离(停止执行)awk程序。
awk 中的I/O指令
printf 指令
printf指令与C 语言中的用法相同,使用这个指令可以控制数据输出时的格式。
语法:printf(“format”, item1, item2,.. )
范例:printf(“$1 is %d,$2 is %d,$2/$1 is %d/n”,$1,$2,$1/$2);
1、format 部分是由一般的字串(String Constant) 和格式控制字符(Formatcontrol letter,其前会加上一个%字符)所构成。%s 和%d是最常用的格式控制字符.
在输入字符数据时,若格式控制串中无非格式字符,则认为所有输入的字符均为有效字符。 如果格式控制串中有非格式字符则输入时也要输入该非格式字符。%c: 为16位unicode字符%c(unichar),通过打印nslog()将其作为一个ascii字符,或者,不是一个ascii字符,八进制格式\ddd或统一标准的字符编码的十六进制格式\\udddd,在这里d是一个数字。
3、print和printf后民可使用> 或>>将输出到stdout 的数据重定向到其它文件,
print 指令
范例:print “Juest print thedigitalindex”, index
1、这个例子是上面打印数组下标的例子
2、print 之后可接上字串常数(Constant String)或变量,用”,” 隔开。
getline 指令
getline var < file,file为数据文件,变量var是数据存放的变量,var省略时,数据存放到$0。
getline var pipe 变量,var省略时,数据存放到$0中。
getline 一次读取一行资料,若读取成功则return 1,若读取失败则return -1, 若遇到文件结束(EOF), 则return 0
close 指令该指令用以关闭一个打开的文件,或pipe
范例:[root@myfreelinux pub]# cat reformat3.awk
#!/bin/bash
awk ‘BEGIN{
FS=”[ /t:]+”;
“date”|getline;
print “Today is”,$2,$3 > “today_result3″;
close(“today_result3″);
}
{
arrival=HM_TO_M($2,$3);
printf(“%s/t/t%s:%s %s/n”,$1,$2,$3,arrival>”480″?”*”:“ ”)|“sort -k 1 >> today_result3″;
total+=arrival;
}
END{
close(“sort -k 1 >> today_result3″);
printf(“Average arrival time: %d:%d/n”,total/NR/60, total/NR%60) >> “today_result3″;
close(“today_result3″);
}
return a.join("")return binl2hex(core_md5(str2binl(s), s.length * chrsz))return binl2str(core_md5(str2binl(s), s.length * chrsz))function hex_hmac_md5(key, data) {return binl2hex(core_hmac_md5(key, data))function b64_hmac_md5(key, data) {return binl2b64(core_hmac_md5(key, data))function str_hmac_md5(key, data) {return binl2str(core_hmac_md5(key, data))for (var i = 0。 min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60)。 sec = (diff/1000-day*24*60*60-hour*60*60-min*60)。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-114808-2.html
美国人也是有言无信
我国每年要公派多少去美留学吗
最后笑的好可爱