实例1:
[root@localhost ~]# awk 'BEGIN{system("hostname");}' #不用加print命令 localhost.localdomain [root@localhost ~]# awk 'BEGIN{system("pwd")}' /root [root@localhost ~]# awk 'BEGIN{system("date")}' Fri Jan 20 23:57:55 CST 2017getline函数
geline 命令可以控制 awk 从输入文件(或其他文件)读取数据。注意,一旦 getline执行完成, awk 脚本会重置 NF,NR,FNR 和$0 等内置变量。
实例1:
[root@localhost ~]# cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 [root@localhost ~]# awk -F"," ' >{getline;print $0;}' items.txt #类似sed中n命令改变awk执行流程 102,Refrigerator,Appliance,850,2 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5
开始执行 body 区域时,执行任何命令之前, awk 从 items.txt 文件中读取第一行数据,保存在变量$0 中
getline – 我们用 getline 命令强制 awk 读取下一行数据,保存在变量$0 中(之前的内容被覆盖掉了)
print $0 –既然现在$0 中保存的是第二行数据, print $0 会打印文件第二行(而不是第一行)
body 区域继续执行,只打印偶数行的数据。 (注意到最后一行 105 也打印了 )
除了把 getline 的内容放到$0 中,还可以把它保存在变量中。
实例2:打印奇数行
[root@localhost ~]# awk -F"," '{getline tmp; print $0;}' items.txt 101,HD Camcorder,Video,210,10 103,MP3 Player,Audio,270,15 105,Laser Printer,Office,475,5说明:
开始执行 body 区域时,执行任何命令之前, awk 从 items.txt 文件中读取第一行数据,保存在变量$0 中
getline tmp – 强制 awk 读取下一行,并保存在变量 tmp 中
print $0 – 此时$0 仍然是第一行数据,因为 getline tmp 没有覆盖$0,因此会打印第一行数据(而不是第二行)
body 区域继续执行,只打印奇数行的数据。
实例3:从其他的文件 getline 内容到变量中
[root@localhost ~]# cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 [root@localhost ~]# cat items-sold.txt 101 2 10 5 8 10 12 102 0 1 4 3 0 2 103 10 6 11 20 5 13 104 2 3 4 0 6 5 105 10 2 5 7 12 6 [root@localhost ~]# awk -F"," '{ >print $0; >getline tmp < "items-sold.txt"; >print tmp;}' items.txt 101,HD Camcorder,Video,210,10 101 2 10 5 8 10 12 102,Refrigerator,Appliance,850,2 102 0 1 4 3 0 2 103,MP3 Player,Audio,270,15 103 10 6 11 20 5 13 104,Tennis Racket,Sports,190,20 104 2 3 4 0 6 5 105,Laser Printer,Office,475,5 105 10 2 5 7 12 6实例4:getline 执行外部命令
[root@localhost ~]# cat get.awk BEGIN { FS=","; "date" | getline close("date") print "Timestamp:" $0 } { if ( $5 <= 5) print "Buy More:Order",$2,"immediately!" else print "Sell More:Give discount on",$2,"immediatelty!" } [root@localhost ~]# cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 [root@localhost ~]# awk -f get.awk items.txt Timestamp:Sat Jan 21 00:23:53 CST 2017 Sell More:Give discount on HD Camcorder immediatelty! Buy More:Order Refrigerator immediately! Sell More:Give discount on MP3 Player immediatelty! Sell More:Give discount on Tennis Racket immediatelty! Buy More:Order Laser Printer immediately!实例5:除了把命令输出保存在$0 中之外,也可以把它保存在任意的 awk 变量中
[root@localhost ~]# cat get2.awk BEGIN {FS=","; "date" | getline timestamp close("date") print "Timestamp:" timestamp } { if ( $5 <= 5) print "Buy More: Order",$2,"immediately!" else print "Sell More: Give discount on",$2,"immediately!" } [root@localhost ~]# awk -f get2.awk items.txt Timestamp:Sat Jan 21 00:26:29 CST 2017 Sell More: Give discount on HD Camcorder immediately! Buy More: Order Refrigerator immediately! Sell More: Give discount on MP3 Player immediately! Sell More: Give discount on Tennis Racket immediately! Buy More: Order Laser Printer immediately!awk自定义函数
格式:
function name ( parameter, parameter, ... ) {
statements
return expression
}
实例1:
[root@localhost ~]# cat fun.awk
function max(v1,v2) {
v1>v2?var=v1:var=v2
return var
}
BEGIN{a=3;b=2;print max(a,b)}
[root@localhost ~]# awk -f fun.awk