awk输出到多个shell变量 [英] awk outputs to multiple shell variables

查看:563
本文介绍了awk输出到多个shell变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的awk程序中有多个print语句,我希望它们将其传递回shell变量.是否有可能.

There are multiple print statements in my awk program and i want them to pass it back to shell variables. Is it possible.

例如:

awk '{ r=10; q=20; rr = sprintf("%04.0f", r); qq = sprintf("%05.0f",q); }'

我可以将rr和qq的输出传递给两个不同的shell变量吗?

Can i pass the output of rr and qq into two different shell variables?

推荐答案

我发现用awk命令的输出填充shell数组是最简单的:

I find it simplest to populate a shell array with the output of the awk command:

$ arr=( $(awk 'BEGIN{ r=10; q=20; printf "%04.0f %05.0f\n",r,q }') )
$ echo "${arr[0]}"
0010
$ echo "${arr[1]}"
00020

然后您可以在数组的shell中做任何您想做的事,例如如果需要,请填充其他变量.

then you can do whatever you like in shell with the array, e.g. populate other variables if you like.

这篇关于awk输出到多个shell变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆