Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put result of awk into an array

When I run the following command on terminal,

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

where,

files=`ls`

I get this output:

2013/08/18 12:51:37,11,724
2013/08/18 12:48:02,227,84769

I wish to create a script, run that command and assign the above result to an array in this way: (Separate lines as separate elements)

array[0] = 2013/08/18 12:51:37,11,724
array[1] = 2013/08/18 12:48:02,227,84769

BUT,

neither,

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

nor,

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

fulfills my purpose.

How to get an array like I specified?

like image 217
coolscitist Avatar asked Mar 20 '23 23:03

coolscitist


1 Answers

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.

like image 148
devnull Avatar answered Apr 05 '23 19:04

devnull