Put result of awk into an array

后端 未结 3 1384
北恋
北恋 2021-01-06 10:33

When I run the following command on terminal,

awk /984/ $files | awk -F, \'{OFS=\",\";print $1,$4,$17}\'

where,

         


        
相关标签:
3条回答
  • 2021-01-06 11:09

    I think the problem is in the usage of () in your script. I tried a similar example and got the required output

    myarray=(`ls *.sh`)
    for f in ${myarray[@]} 
    do
        echo $f
    done
    

    I think the code of yours should be changed as follows

    result=(`awk /string/ $files | awk -F, '{OFS=",";print $1,$4,$17}'`)
    
    0 讨论(0)
  • 2021-01-06 11:14

    To store variables in an array, output needs to be in a form like this:

    result=$(one two "more data")
    echo ${result[2]}
    more data
    

    Data separated by spaces. So tweak your output to give that format.

    Can you give an example of what you get out of:

    awk '/984/' $files | awk -F, '{OFS=",";print $1,$4,$17}'
    

    It may be shorten to:

    awk -F, '/984/ {OFS=",";print $1,$4,$17}' $files
    
    0 讨论(0)
  • 2021-01-06 11:15

    When you say:

    result=($(awk /string/ $files | awk -F, '{OFS=",";print $1,$4,$17}'))
    

    the output would be split by whitespace. Set IFS to a newline character, and you should see the desired result. Say:

    IFS=$'\n' result=($(awk /string/ $files | awk -F, '{OFS=",";print $1,$4,$17}'))
    

    instead to capture different lines of output into an array.

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