diff --git a/CONTRIBUTORS.svg b/CONTRIBUTORS.svg index e1386852fb..abdbb37148 100644 --- a/CONTRIBUTORS.svg +++ b/CONTRIBUTORS.svg @@ -116,89 +116,89 @@ + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - + \ No newline at end of file diff --git a/c/7z.html b/c/7z.html index 2c2f0b2b58..a66641545f 100644 --- a/c/7z.html +++ b/c/7z.html @@ -9,7 +9,7 @@ 7z 命令,Linux 7z 命令详解:拥有极高压缩比的开源压缩软件。 - Linux 命令搜索引擎 - + @@ -217,7 +217,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/ab.html b/c/ab.html index 81ef9110dd..1ac47c1164 100644 --- a/c/ab.html +++ b/c/ab.html @@ -9,7 +9,7 @@ ab 命令,Linux ab 命令详解:Apache服务器的性能测试工具 - Linux 命令搜索引擎 - + @@ -388,7 +388,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/accept.html b/c/accept.html index a28cbb7088..7f9b53626f 100644 --- a/c/accept.html +++ b/c/accept.html @@ -9,7 +9,7 @@ accept 命令,Linux accept 命令详解:指示打印系统接受发往指定目标打印机的打印任务 - Linux 命令搜索引擎 - + @@ -202,7 +202,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/ack.html b/c/ack.html index 0a061bcb58..9bd91db97c 100644 --- a/c/ack.html +++ b/c/ack.html @@ -9,7 +9,7 @@ ack 命令,Linux ack 命令详解:比grep好用的文本搜索工具 - Linux 命令搜索引擎 - + @@ -276,7 +276,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/ag.html b/c/ag.html index 9bc895b283..2e37b5eb8c 100644 --- a/c/ag.html +++ b/c/ag.html @@ -9,7 +9,7 @@ ag 命令,Linux ag 命令详解:ack 的升级版,C语言编写,更快更人性化 - Linux 命令搜索引擎 - + @@ -360,7 +360,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/alias.html b/c/alias.html index 66eb7a789c..093791a9da 100644 --- a/c/alias.html +++ b/c/alias.html @@ -9,7 +9,7 @@ alias 命令,Linux alias 命令详解:定义或显示别名。 - Linux 命令搜索引擎 - + @@ -309,7 +309,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/apachectl.html b/c/apachectl.html index a9b7644f20..15a9c8bf94 100644 --- a/c/apachectl.html +++ b/c/apachectl.html @@ -9,7 +9,7 @@ apachectl 命令,Linux apachectl 命令详解:Apache服务器前端控制工具 - Linux 命令搜索引擎 - + @@ -203,7 +203,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/apk.html b/c/apk.html index 918471f1be..f4413852a0 100644 --- a/c/apk.html +++ b/c/apk.html @@ -9,7 +9,7 @@ apk 命令,Linux apk 命令详解:Alpine Linux 下的包管理工具 - Linux 命令搜索引擎 - + @@ -391,7 +391,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/apropos.html b/c/apropos.html index 3836dc08fc..5f42ae5778 100644 --- a/c/apropos.html +++ b/c/apropos.html @@ -9,7 +9,7 @@ apropos 命令,Linux apropos 命令详解:在 whatis 数据库中查找字符串 - Linux 命令搜索引擎 - + @@ -273,7 +273,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/apt-get.html b/c/apt-get.html index 15490b5b0b..cf29651b89 100644 --- a/c/apt-get.html +++ b/c/apt-get.html @@ -9,7 +9,7 @@ apt-get 命令,Linux apt-get 命令详解:Debian Linux发行版中的APT软件包管理工具 - Linux 命令搜索引擎 - + @@ -263,7 +263,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/apt-key.html b/c/apt-key.html index c177856ac4..8e09a87550 100644 --- a/c/apt-key.html +++ b/c/apt-key.html @@ -9,7 +9,7 @@ apt-key 命令,Linux apt-key 命令详解:管理Debian Linux系统中的软件包密钥 - Linux 命令搜索引擎 - + @@ -204,7 +204,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/apt-sortpkgs.html b/c/apt-sortpkgs.html index 953f0d5da4..7d14968ab7 100644 --- a/c/apt-sortpkgs.html +++ b/c/apt-sortpkgs.html @@ -9,7 +9,7 @@ apt-sortpkgs 命令,Linux apt-sortpkgs 命令详解:Debian Linux下对软件包索引文件进行排序的工具 - Linux 命令搜索引擎 - + @@ -200,7 +200,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/aptitude.html b/c/aptitude.html index b86f3437a0..9369e817af 100644 --- a/c/aptitude.html +++ b/c/aptitude.html @@ -9,7 +9,7 @@ aptitude 命令,Linux aptitude 命令详解:Debian Linux系统中软件包管理工具 - Linux 命令搜索引擎 - + @@ -232,7 +232,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/ar.html b/c/ar.html index 4aeb2e024a..9e98501033 100644 --- a/c/ar.html +++ b/c/ar.html @@ -9,7 +9,7 @@ ar 命令,Linux ar 命令详解:建立或修改备存文件,或是从备存文件中抽取文件 - Linux 命令搜索引擎 - + @@ -424,7 +424,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/arch.html b/c/arch.html index fbdd45d3e1..7d6cf16141 100644 --- a/c/arch.html +++ b/c/arch.html @@ -9,7 +9,7 @@ arch 命令,Linux arch 命令详解:显示当前主机的硬件架构类型 - Linux 命令搜索引擎 - + @@ -215,7 +215,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/arj.html b/c/arj.html index a92ebacbaf..216009b4c0 100644 --- a/c/arj.html +++ b/c/arj.html @@ -9,7 +9,7 @@ arj 命令,Linux arj 命令详解:用于创建和管理.arj压缩包 - Linux 命令搜索引擎 - + @@ -197,7 +197,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/arp.html b/c/arp.html index 2052232f7a..016102c9ea 100644 --- a/c/arp.html +++ b/c/arp.html @@ -9,7 +9,7 @@ arp 命令,Linux arp 命令详解:arp 命令用于显示和修改 IP 到 MAC 转换表 - Linux 命令搜索引擎 - + @@ -245,7 +245,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/arpd.html b/c/arpd.html index b9d2e4e889..f1c341e179 100644 --- a/c/arpd.html +++ b/c/arpd.html @@ -9,7 +9,7 @@ arpd 命令,Linux arpd 命令详解:收集免费ARP信息 - Linux 命令搜索引擎 - + @@ -217,7 +217,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/arping.html b/c/arping.html index 6d6370bb9e..5994d9913b 100644 --- a/c/arping.html +++ b/c/arping.html @@ -9,7 +9,7 @@ arping 命令,Linux arping 命令详解:通过发送ARP协议报文测试网络 - Linux 命令搜索引擎 - + @@ -236,7 +236,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/arptables.html b/c/arptables.html index 8fa6e38cb5..0b89fac5d7 100644 --- a/c/arptables.html +++ b/c/arptables.html @@ -9,7 +9,7 @@ arptables 命令,Linux arptables 命令详解:管理ARP包过滤规则表 - Linux 命令搜索引擎 - + @@ -220,7 +220,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/arpwatch.html b/c/arpwatch.html index bdd3c4d2c6..426437fdc1 100644 --- a/c/arpwatch.html +++ b/c/arpwatch.html @@ -9,7 +9,7 @@ arpwatch 命令,Linux arpwatch 命令详解:监听网络上ARP的记录 - Linux 命令搜索引擎 - + @@ -202,7 +202,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/as.html b/c/as.html index 965410e4d3..540ce0140a 100644 --- a/c/as.html +++ b/c/as.html @@ -9,7 +9,7 @@ as 命令,Linux as 命令详解:汇编语言编译器 - Linux 命令搜索引擎 - + @@ -226,7 +226,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/at.html b/c/at.html index 3f34163ab2..8b1a7216e3 100644 --- a/c/at.html +++ b/c/at.html @@ -9,7 +9,7 @@ at 命令,Linux at 命令详解:在指定时间执行一个任务 - Linux 命令搜索引擎 - + @@ -266,7 +266,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/atop.html b/c/atop.html index ff541a1c6d..fc549c83c5 100644 --- a/c/atop.html +++ b/c/atop.html @@ -9,7 +9,7 @@ atop 命令,Linux atop 命令详解:监控Linux系统资源与进程的工具 - Linux 命令搜索引擎 - + @@ -262,7 +262,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/atq.html b/c/atq.html index aa75ea67d3..8cf16bef26 100644 --- a/c/atq.html +++ b/c/atq.html @@ -9,7 +9,7 @@ atq 命令,Linux atq 命令详解:列出当前用户的at任务列表 - Linux 命令搜索引擎 - + @@ -214,7 +214,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/atrm.html b/c/atrm.html index 1b2bc2a9d5..6a3fc52bf7 100644 --- a/c/atrm.html +++ b/c/atrm.html @@ -9,7 +9,7 @@ atrm 命令,Linux atrm 命令详解:删除待执行任务队列中的指定任务 - Linux 命令搜索引擎 - + @@ -211,7 +211,7 @@ function copied(target, str) { 586 个Linux命令,超过 50+ 贡献者 - - + + \ No newline at end of file diff --git a/c/awk.html b/c/awk.html index 4aa27fc97f..9e4a0cb288 100644 --- a/c/awk.html +++ b/c/awk.html @@ -9,7 +9,7 @@ awk 命令,Linux awk 命令详解:文本和数据进行处理的编程语言 - Linux 命令搜索引擎 - + @@ -340,36 +340,36 @@ echo | awk '{ print v1,v2 }' v1=$var1 v2=$var2

awk运算与判断

作为一种程序设计语言所应具有的特点之一,awk支持多种运算,这些运算与C语言提供的基本相同。awk还提供了一系列内置的运算函数(如log、sqr、cos、sin等)和一些用于对字符串进行操作(运算)的函数(如length、substr等等)。这些函数的引用大大的提高了awk的运算功能。作为对条件转移指令的一部分,关系判断是每种程序设计语言都具备的功能,awk也不例外,awk中允许进行多种测试,作为样式匹配,还提供了模式匹配表达式~(匹配)和!~(不匹配)。作为对测试的一种扩充,awk也支持用逻辑运算符。

算术运算符

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
运算符描述
+ -加,减
* / &乘,除与求余
+ - !一元加,减和逻辑非
^ ***求幂
++ --增加或减少,作为前缀或后缀
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
运算符描述
+ -加,减
* / &乘,除与求余
+ - !一元加,减和逻辑非
^ ***求幂
++ --增加或减少,作为前缀或后缀

例:

awk 'BEGIN{a="b";print a++,++a;}'
 0 2
@@ -378,43 +378,43 @@ echo | awk '{ print v1,v2 }' v1=$var1 v2=$var2
 " class="copied">

注意:所有用作算术运算符进行操作,操作数自动转为数值,所有非数值都变为0

赋值运算符

- - - - - - - - - - - - - -
运算符描述
= += -= *= /= %= ^= **=赋值语句
+ + + + + + + + + + + + + +
运算符描述
= += -= *= /= %= ^= **=赋值语句

例:

a+=5; 等价于:a=a+5; 其它同类
 

逻辑运算符

- - - - - - - - - - - - - - - - - -
运算符描述
||逻辑或
&&逻辑与
+ + + + + + + + + + + + + + + + + +
运算符描述
||逻辑或
&&逻辑与

例:

awk 'BEGIN{a=1;b=2;print (a>5 && b<=2),(a>5 || b<=2);}'
 0 1
@@ -422,20 +422,20 @@ echo | awk '{ print v1,v2 }' v1=$var1 v2=$var2
 0 1
 " class="copied">

正则运算符

- - - - - - - - - - - - - -
运算符描述
~ !~匹配正则表达式和不匹配正则表达式
+ + + + + + + + + + + + + +
运算符描述
~ !~匹配正则表达式和不匹配正则表达式
^ 行首
 $ 行尾
 . 除了换行符以外的任意单个字符
@@ -475,20 +475,20 @@ $ 行尾
 ok
 " class="copied">

关系运算符

- - - - - - - - - - - - - -
运算符描述
< <= > >= != ==关系运算符
+ + + + + + + + + + + + + +
运算符描述
< <= > >= != ==关系运算符

例:

awk 'BEGIN{a=11;if(a >= 9){print "ok";}}'
 ok
@@ -497,32 +497,32 @@ ok
 " class="copied">

注意:> < 可以作为字符串比较,也可以用作数值比较,关键看操作数如果是字符串就会转换为字符串比较。两个都为数字才转为数值比较。字符串比较:按照ASCII码顺序比较。

其它运算符

- - - - - - - - - - - - - - - - - - - - - - - - - -
运算符描述
$字段引用
空格字符串连接符
?:C条件表达式
in数组中是否存在某键值
+ + + + + + + + + + + + + + + + + + + + + + + + + +
运算符描述
$字段引用
空格字符串连接符
?:C条件表达式
in数组中是否存在某键值

例:

awk 'BEGIN{a="b";print a=="b"?"ok":"err";}'
 ok
@@ -994,52 +994,52 @@ for(m in tarr){
 

内置函数

awk内置函数,主要分以下3种类似:算数函数、字符串函数、其它一般函数、时间函数。

算术函数

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
格式描述
atan2( y, x )返回 y/x 的反正切。
cos( x )返回 x 的余弦;x 是弧度。
sin( x )返回 x 的正弦;x 是弧度。
exp( x )返回 x 幂函数。
log( x )返回 x 的自然对数。
sqrt( x )返回 x 平方根。
int( x )返回 x 的截断至整数的值。
rand( )返回任意数字 n,其中 0 <= n < 1。
srand( [expr] )将 rand 函数的种子值设置为 Expr 参数的值,或如果省略 Expr 参数则使用某天的时间。返回先前的种子值。
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
格式描述
atan2( y, x )返回 y/x 的反正切。
cos( x )返回 x 的余弦;x 是弧度。
sin( x )返回 x 的正弦;x 是弧度。
exp( x )返回 x 幂函数。
log( x )返回 x 的自然对数。
sqrt( x )返回 x 平方根。
int( x )返回 x 的截断至整数的值。
rand( )返回任意数字 n,其中 0 <= n < 1。
srand( [expr] )将 rand 函数的种子值设置为 Expr 参数的值,或如果省略 Expr 参数则使用某天的时间。返回先前的种子值。

举例说明:

awk 'BEGIN{OFMT="%.3f";fs=sin(1);fe=exp(10);fl=log(10);fi=int(3.1415);print fs,fe,fl,fi;}'
 0.841 22026.466 2.303 3
@@ -1064,60 +1064,60 @@ awk 'BEGIN{srand();fr=int(100*rand());print fr;}'
 41 
 " class="copied">

字符串函数

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
格式描述
gsub( Ere, Repl, [ In ] )除了正则表达式所有具体值被替代这点,它和 sub 函数完全一样地执行。
sub( Ere, Repl, [ In ] )用 Repl 参数指定的字符串替换 In 参数指定的字符串中的由 Ere 参数指定的扩展正则表达式的第一个具体值。sub 函数返回替换的数量。出现在 Repl 参数指定的字符串中的 &(和符号)由 In 参数指定的与 Ere 参数的指定的扩展正则表达式匹配的字符串替换。如果未指定 In 参数,缺省值是整个记录($0 记录变量)。
index( String1, String2 )在由 String1 参数指定的字符串(其中有出现 String2 指定的参数)中,返回位置,从 1 开始编号。如果 String2 参数不在 String1 参数中出现,则返回 0(零)。
length [(String)]返回 String 参数指定的字符串的长度(字符形式)。如果未给出 String 参数,则返回整个记录的长度($0 记录变量)。
blength [(String)]返回 String 参数指定的字符串的长度(以字节为单位)。如果未给出 String 参数,则返回整个记录的长度($0 记录变量)。
substr( String, M, [ N ] )返回具有 N 参数指定的字符数量子串。子串从 String 参数指定的字符串取得,其字符以 M 参数指定的位置开始。M 参数指定为将 String 参数中的第一个字符作为编号 1。如果未指定 N 参数,则子串的长度将是 M 参数指定的位置到 String 参数的末尾 的长度。
match( String, Ere )在 String 参数指定的字符串(Ere 参数指定的扩展正则表达式出现在其中)中返回位置(字符形式),从 1 开始编号,或如果 Ere 参数不出现,则返回 0(零)。RSTART 特殊变量设置为返回值。RLENGTH 特殊变量设置为匹配的字符串的长度,或如果未找到任何匹配,则设置为 -1(负一)。
split( String, A, [Ere] )将 String 参数指定的参数分割为数组元素 A[1], A[2], . . ., A[n],并返回 n 变量的值。此分隔可以通过 Ere 参数指定的扩展正则表达式进行,或用当前字段分隔符(FS 特殊变量)来进行(如果没有给出 Ere 参数)。除非上下文指明特定的元素还应具有一个数字值,否则 A 数组中的元素用字符串值来创建。
tolower( String )返回 String 参数指定的字符串,字符串中每个大写字符将更改为小写。大写和小写的映射由当前语言环境的 LC_CTYPE 范畴定义。
toupper( String )返回 String 参数指定的字符串,字符串中每个小写字符将更改为大写。大写和小写的映射由当前语言环境的 LC_CTYPE 范畴定义。
sprintf(Format, Expr, Expr, . . . )根据 Format 参数指定的 printf 子例程格式字符串来格式化 Expr 参数指定的表达式并返回最后生成的字符串。
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
格式描述
gsub( Ere, Repl, [ In ] )除了正则表达式所有具体值被替代这点,它和 sub 函数完全一样地执行。
sub( Ere, Repl, [ In ] )用 Repl 参数指定的字符串替换 In 参数指定的字符串中的由 Ere 参数指定的扩展正则表达式的第一个具体值。sub 函数返回替换的数量。出现在 Repl 参数指定的字符串中的 &(和符号)由 In 参数指定的与 Ere 参数的指定的扩展正则表达式匹配的字符串替换。如果未指定 In 参数,缺省值是整个记录($0 记录变量)。
index( String1, String2 )在由 String1 参数指定的字符串(其中有出现 String2 指定的参数)中,返回位置,从 1 开始编号。如果 String2 参数不在 String1 参数中出现,则返回 0(零)。
length [(String)]返回 String 参数指定的字符串的长度(字符形式)。如果未给出 String 参数,则返回整个记录的长度($0 记录变量)。
blength [(String)]返回 String 参数指定的字符串的长度(以字节为单位)。如果未给出 String 参数,则返回整个记录的长度($0 记录变量)。
substr( String, M, [ N ] )返回具有 N 参数指定的字符数量子串。子串从 String 参数指定的字符串取得,其字符以 M 参数指定的位置开始。M 参数指定为将 String 参数中的第一个字符作为编号 1。如果未指定 N 参数,则子串的长度将是 M 参数指定的位置到 String 参数的末尾 的长度。
match( String, Ere )在 String 参数指定的字符串(Ere 参数指定的扩展正则表达式出现在其中)中返回位置(字符形式),从 1 开始编号,或如果 Ere 参数不出现,则返回 0(零)。RSTART 特殊变量设置为返回值。RLENGTH 特殊变量设置为匹配的字符串的长度,或如果未找到任何匹配,则设置为 -1(负一)。
split( String, A, [Ere] )将 String 参数指定的参数分割为数组元素 A[1], A[2], . . ., A[n],并返回 n 变量的值。此分隔可以通过 Ere 参数指定的扩展正则表达式进行,或用当前字段分隔符(FS 特殊变量)来进行(如果没有给出 Ere 参数)。除非上下文指明特定的元素还应具有一个数字值,否则 A 数组中的元素用字符串值来创建。
tolower( String )返回 String 参数指定的字符串,字符串中每个大写字符将更改为小写。大写和小写的映射由当前语言环境的 LC_CTYPE 范畴定义。
toupper( String )返回 String 参数指定的字符串,字符串中每个小写字符将更改为大写。大写和小写的映射由当前语言环境的 LC_CTYPE 范畴定义。
sprintf(Format, Expr, Expr, . . . )根据 Format 参数指定的 printf 子例程格式字符串来格式化 Expr 参数指定的表达式并返回最后生成的字符串。

注:Ere都可以是正则表达式。

gsub,sub使用

awk 'BEGIN{info="this is a test2010test!";gsub(/[0-9]+/,"!",info);print info}'
@@ -1164,84 +1164,84 @@ s is a tes
 

格式化字符串输出(sprintf使用)

格式化字符串格式:

其中格式化字符串包括两部分内容:一部分是正常字符,这些字符将按原样输出; 另一部分是格式化规定字符,以"%"开始,后跟一个或几个规定字符,用来确定输出内容格式。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
格式描述格式描述
%d十进制有符号整数%u十进制无符号整数
%f浮点数%s字符串
%c单个字符%p指针的值
%e指数形式的浮点数%x%X 无符号以十六进制表示的整数
%o无符号以八进制表示的整数%g自动选择合适的表示法
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
格式描述格式描述
%d十进制有符号整数%u十进制无符号整数
%f浮点数%s字符串
%c单个字符%p指针的值
%e指数形式的浮点数%x%X 无符号以十六进制表示的整数
%o无符号以八进制表示的整数%g自动选择合适的表示法
awk 'BEGIN{n1=124.113;n2=-1.224;n3=1.2345; printf("%.2f,%.2u,%.2g,%X,%on",n1,n2,n3,n1,n1);}'
 124.11,18446744073709551615,1.2,7C,174
 

一般函数

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
格式描述
close( Expression )用同一个带字符串值的 Expression 参数来关闭由 print 或 printf 语句打开的或调用 getline 函数打开的文件或管道。如果文件或管道成功关闭,则返回 0;其它情况下返回非零值。如果打算写一个文件,并稍后在同一个程序中读取文件,则 close 语句是必需的。
system(command )执行 Command 参数指定的命令,并返回退出状态。等同于 system 子例程。
Expression | getline [ Variable ]从来自 Expression 参数指定的命令的输出中通过管道传送的流中读取一个输入记录,并将该记录的值指定给 Variable 参数指定的变量。如果当前未打开将 Expression 参数的值作为其命令名称的流,则创建流。创建的流等同于调用 popen 子例程,此时 Command 参数取 Expression 参数的值且 Mode 参数设置为一个是 r 的值。只要流保留打开且 Expression 参数求得同一个字符串,则对 getline 函数的每次后续调用读取另一个记录。如果未指定 Variable 参数,则 $0 记录变量和 NF 特殊变量设置为从流读取的记录。
getline [ Variable ] < Expression从 Expression 参数指定的文件读取输入的下一个记录,并将 Variable 参数指定的变量设置为该记录的值。只要流保留打开且 Expression 参数对同一个字符串求值,则对 getline 函数的每次后续调用读取另一个记录。如果未指定 Variable 参数,则 $0 记录变量和 NF 特殊变量设置为从流读取的记录。
getline [ Variable ]将 Variable 参数指定的变量设置为从当前输入文件读取的下一个输入记录。如果未指定 Variable 参数,则 $0 记录变量设置为该记录的值,还将设置 NF、NR 和 FNR 特殊变量。
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
格式描述
close( Expression )用同一个带字符串值的 Expression 参数来关闭由 print 或 printf 语句打开的或调用 getline 函数打开的文件或管道。如果文件或管道成功关闭,则返回 0;其它情况下返回非零值。如果打算写一个文件,并稍后在同一个程序中读取文件,则 close 语句是必需的。
system(command )执行 Command 参数指定的命令,并返回退出状态。等同于 system 子例程。
Expression | getline [ Variable ]从来自 Expression 参数指定的命令的输出中通过管道传送的流中读取一个输入记录,并将该记录的值指定给 Variable 参数指定的变量。如果当前未打开将 Expression 参数的值作为其命令名称的流,则创建流。创建的流等同于调用 popen 子例程,此时 Command 参数取 Expression 参数的值且 Mode 参数设置为一个是 r 的值。只要流保留打开且 Expression 参数求得同一个字符串,则对 getline 函数的每次后续调用读取另一个记录。如果未指定 Variable 参数,则 $0 记录变量和 NF 特殊变量设置为从流读取的记录。
getline [ Variable ] < Expression从 Expression 参数指定的文件读取输入的下一个记录,并将 Variable 参数指定的变量设置为该记录的值。只要流保留打开且 Expression 参数对同一个字符串求值,则对 getline 函数的每次后续调用读取另一个记录。如果未指定 Variable 参数,则 $0 记录变量和 NF 特殊变量设置为从流读取的记录。
getline [ Variable ]将 Variable 参数指定的变量设置为从当前输入文件读取的下一个输入记录。如果未指定 Variable 参数,则 $0 记录变量设置为该记录的值,还将设置 NF、NR 和 FNR 特殊变量。

打开外部文件(close用法)

awk 'BEGIN{while("cat /etc/passwd"|getline){print $0;};close("/etc/passwd");}'
 root:x:0:0:root:/root:/bin/bash
@@ -1283,32 +1283,32 @@ drwxr-xr-x 95 root   root       4096 10-08 14:01 ..
 " class="copied">

b返回值,是执行结果。

时间函数

- - - - - - - - - - - - - - - - - - - - - - - - - -
格式描述
函数名说明
mktime( YYYY MM dd HH MM ss[ DST])生成时间格式
strftime([format [, timestamp]])格式化时间输出,将时间戳转为时间字符串具体格式,见下表。
systime()得到时间戳,返回从1970年1月1日开始到当前时间(不计闰年)的整秒数
+ + + + + + + + + + + + + + + + + + + + + + + + + +
格式描述
函数名说明
mktime( YYYY MM dd HH MM ss[ DST])生成时间格式
strftime([format [, timestamp]])格式化时间输出,将时间戳转为时间字符串具体格式,见下表。
systime()得到时间戳,返回从1970年1月1日开始到当前时间(不计闰年)的整秒数

建指定时间(mktime使用)

awk 'BEGIN{tstamp=mktime("2001 01 01 12 12 12");print strftime("%c",tstamp);}'
 2001年01月01日 星期一 12时12分12秒
@@ -1327,108 +1327,108 @@ drwxr-xr-x 95 root   root       4096 10-08 14:01 ..
 308201392
 " class="copied">

strftime日期和时间格式说明符

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
格式描述
%a星期几的缩写(Sun)
%A星期几的完整写法(Sunday)
%b月名的缩写(Oct)
%B月名的完整写法(October)
%c本地日期和时间
%d十进制日期
%D日期 08/20/99
%e日期,如果只有一位会补上一个空格
%H用十进制表示24小时格式的小时
%I用十进制表示12小时格式的小时
%j从1月1日起一年中的第几天
%m十进制表示的月份
%M十进制表示的分钟
%p12小时表示法(AM/PM)
%S十进制表示的秒
%U十进制表示的一年中的第几个星期(星期天作为一个星期的开始)
%w十进制表示的星期几(星期天是0)
%W十进制表示的一年中的第几个星期(星期一作为一个星期的开始)
%x重新设置本地日期(08/20/99)
%X重新设置本地时间(12:00:00)
%y两位数字表示的年(99)
%Y当前月份
%%百分号(%)
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
格式描述
%a星期几的缩写(Sun)
%A星期几的完整写法(Sunday)
%b月名的缩写(Oct)
%B月名的完整写法(October)
%c本地日期和时间
%d十进制日期
%D日期 08/20/99
%e日期,如果只有一位会补上一个空格
%H用十进制表示24小时格式的小时
%I用十进制表示12小时格式的小时
%j从1月1日起一年中的第几天
%m十进制表示的月份
%M十进制表示的分钟
%p12小时表示法(AM/PM)
%S十进制表示的秒
%U十进制表示的一年中的第几个星期(星期天作为一个星期的开始)
%w十进制表示的星期几(星期天是0)
%W十进制表示的一年中的第几个星期(星期一作为一个星期的开始)
%x重新设置本地日期(08/20/99)
%X重新设置本地时间(12:00:00)
%y两位数字表示的年(99)
%Y当前月份
%%百分号(%)