返回AWK两个变量 [英] Return two variables in awk

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

问题描述

在这里的那一刻是即时通讯做

At the moment here is what im doing

ret=$(ls -la | awk '{print $3 " "  $9}')
usr=$(echo $ret | awk '{print $1}')
fil=$(echo $ret | awk '{print $2}')

问题是,我不是运行 LS 进出口运行,需要时间的命令,所以你可以理解的逻辑。

The problem is that im not running an ls im running a command that takes time, so you can understand the logic.

有没有办法,我可以设置返回值来设置两个外部值,从而

Is there a way I can set the return value to set two external values, so something such as

ls -la | awk -r usr=x -r fil=y '{x=$3; y=$9}'

这样的命令将被运行一次,我可以把它最大限度地减少了一行

This way the command will be run once and i can minimize it to one line

推荐答案

使用读一种解决方法

usr=""
fil=""
while read u f; do usr="$usr\n$u"; fil="$fil\n$f"; done < <(ls -la | awk '{print $3 " "  $9}')

有关性能问题,你可以使用&LT;&LT;&LT; ,但要避免它,如果返回的文本很大:

For performance issue you could use <<<, but avoid it if the returned text is large:

while read u f; do usr="$usr\n$u"; fil="$fil\n$f"; done <<< $(ls -la | awk '{print $3 " "  $9}')

从@ WilliamPursell的回答启发更可移植的方式:

A more portable way inspired from @WilliamPursell's answer:

$ usr=""
$ fil=""
$ while read u f; do usr="$usr\n$u"; fil="$fil\n$f"; done << EOF
> $(ls -la | awk '{print $3 " "  $9}')
> EOF

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

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