awk实战10-IO函数-其他介绍

awk函数-IO函数-其他

本节主要介绍awk io除了getline和close的其他一些基础函数;

1 system-关注指数:☆☆☆☆☆

system(cmd-line) Execute the command cmd-line, and return the exit
​ status. (This may not be available on non-POSIX systems.)

调用系统命令行,并直接打印相关结果,返回值代表是否成功!

返回结果不是打印命令执行打印,而是是否运行成功的打印!

如果命令存在且正常运行则返回0,其他情况返回错误信息;

##### 1-正常运行命令
user@user☆ :~$ awk 'BEGIN{a=system("df");print "----:",a}'
Filesystem     1K-blocks      Used Available Use% Mounted on
udev             3995616         0   3995616   0% /dev
tmpfs             805160     34380    770780   5% /run
/dev/sdb4       19323588  16265512   2053432  89% /
/dev/sdb5       48160632  10093896  35613592  23% /usr
tmpfs            4025792     28336   3997456   1% /dev/shm
tmpfs               5120         4      5116   1% /run/lock
tmpfs            4025792         0   4025792   0% /sys/fs/cgroup
/dev/sdb2         968312    173116    728792  20% /boot
/dev/sdb1         523248      1196    522052   1% /boot/efi
/dev/sda5      210263488 119029656  80530012  60% /home
tmpfs             805160        64    805096   1% /run/user/1000
----: 0
###### 2-命令无法识别:随便输入一个字符串则返回非0
user@user☆ :~$ awk 'BEGIN{a=system("xxx");print "----:",a}'
sh: 1: xxx: not found
----: 127
###### 3-命令执行错误:返回非0
user@user☆ :~$ awk 'BEGIN{a=system("cp test1 ../");print "----:",a}'
cp: cannot stat 'test1': No such file or directory
----: 1

特别说明:system()使用引号来引入外部命令和环境变量,如果使用awk内部变量,则不能带引号

如下所示为一个使用案例:

##### 1-准备数据内容
user@user:~$ cat 1
1-a
user@user:~$ cat 2
2-b
##### 2-awk system使用内部变量
user@user:~$ seq 5 | awk -v var=2 '/1/{system("cat "$1" "var)}'
1-a
2-b
fwd@fwd:~$ seq 5 | awk -v var=2 '/2/{system("cat "$1" "var);system("echo "NR" "NF)}'
2-b
2-b
2 1

2 next 和 nextfile-关注指数:☆☆☆☆

next Stop processing the current input record. The next
​ input record is read and processing starts over with
​ the first pattern in the AWK program. Upon reaching
​ the end of the input data, gawk executes any END

nextfile top processing the current input file. The next
​ input record read comes from the next input file.
​ FILENAME and ARGIND are updated, FNR is reset to 1,
​ and processing starts over with the first pattern in
​ the AWK program. Upon reaching the end of the input
​ data, gawk executes any END rule(s).

next为跳过当前行执行下一行输入和匹配,而nextfile是awk在执行多个文件时候,直接取消当前文件执行的剩余操作,直接开始下一个文件!

准备测试文件test1和test2

user@user:~$ cat test1
1---a
1---b
1---c
user@user:~$ cat test2
2---a
2---b
2---c

test1匹配到1----b后直接跳过了文件进行下一个文件的匹配;

user@user:~$ awk '/1---b/{nextfile}1' test1 test2
1---a
2---a
2---b
2---c

3 fflush-关注指数:☆☆☆☆

fflush([file]) Flush any buffers associated with the open output
​ file or pipe file. If file is missing or if it is
​ the null string, then flush all open output files and
​ pipes.

fflush是针对缓存处理的函数,平时并不影响我们的查看,一直不知道其真正的含义,在这篇博客中得到了一些思路:

以下内容引自博客:https://www.cnblogs.com/cobbliu/p/4954146.html
--------------------------------------------------------------
当使用 
tail -f test.log | grep "mode" | awk '{print $5}'命令
或者 tail -f test.log | awk '/mode/ {print $5}'的时候,如果test.log中满足模式mode的数据很少,会发现即便是test.log中新出现了满足mode的行,但是上面两个命令都没有任何输出。
原因在于grep和awk处于效率的考量,会缓存一批数据再输出到标准输出。
grep的--line-buffered选项和awk的fflush(stdout)命令可以使得grep和awk不缓存数据。如:
tail -f test.log | grep --line-buffered "mode" | awk '{print $5}'
tail -f test.log | awk '/mode/ {print $5,$6; fflush(stdout)}''

4 print-关注指数:☆☆☆☆

print Print the current record. The output record is ter‐
​ minated with the value of ORS.

print expr-list Print expressions. Each expression is separated by
​ the value of OFS. The output record is terminated
​ with the value of ORS.

print expr-list >file Print expressions on file. Each expression is sepa‐
​ rated by the value of OFS. The output record is ter‐
​ minated with the value of ORS.

printf fmt, expr-list Format and print. See The printf Statement, below.

printf fmt, expr-list >file
​ Format and print on file.

print … >> file
​ Appends output to the file.

print … | command
​ Writes on a pipe.

print … |& command
​ Sends data to a co-process or socket. (See also the subsection Spe‐
​ cial File Names, below.)

print和printf是awk 编程中使用最简单也是用的最多的函数,这里仅给出英文说明,基本上所有的用例都会和这两个函数或多或少打交道,在此不再单独举例,仅列出printf的常用转义符表,以作备忘。

printf转义字符

转义字符	定义
c 	字符
s 	字符串
d 	十进制整数
ld 	十进制长整数
u 	十进制无符号整数
lu 	十进制无符号长整数
x 	十六进制整数
lx 	十六进制长整数
o 	八进制整数
lo 	八进制长整数
e 	用科学记数法(e 记数法)表示的浮点数
f 	浮点数
g 	选用e或f中较短的一种形式

printf修饰符

- 	左对齐修饰符
# 	显示8 进制整数时在前面加个0
      显示16 进制整数时在前面加0x
+ 	显示使用d 、e 、f 和g 转换的整数时,加上正负号+或-
0 	用0而不是空白符来填充所显示的值
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值