awk outputs to multiple shell variables

后端 未结 2 1487
遇见更好的自我
遇见更好的自我 2020-12-19 20:00

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

For eg:

awk \'{ r=10; q=20;

相关标签:
2条回答
  • 2020-12-19 20:47

    The bash read statement can be used to capture awk output into shell variables:

    $ read rr qq <<<$(awk 'BEGIN{ r=10; q=20; rr = sprintf("%04.0f", r); qq = sprintf("%05.0f",q); print rr,qq}')
    $ echo $rr $qq
    0010 00020
    
    0 讨论(0)
  • 2020-12-19 20:57

    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
    

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

    0 讨论(0)
提交回复
热议问题