Bash – Use awk to split line into array and use that array’s values in calling shell

awkbashscripting

I am trying to use awk inside a bash script and do a rather common task: iterate well structured file lines and split them by a delimiter into an array. Here is a sample from the file:

Joe:Johnson:25
Sue:Miller:27

There are numerous examples how this can be done on a single line in the interactive mode, however, I am doing it in a script in which I would like to use that array manipulated by awk outside the awk subshell in bash itself:

cat ${smryfile} | while read smryline; do

    echo ${smryline}

    #now i want to split the line into array 'linearray' in awk but have it usable when i get back to bash
    echo ${smryline} | awk '{split($0,$linearray,":")}'

    varX=$linearray[2]
    echo $varX
    #do something with $varX

done

I get an error:

awk: syntax error at source line 1
 context is
     >>> {split($0,$linearray <<< ,":")}
awk: illegal statement at source line 1

Is it possible to do what I am trying to do (use arrays that are defined in awk outside of its scope) and how should I do it?

Best Answer

I think that you can do what you want without awk:

cat "${smryfile}" | while IFS=: read first last varx
do
    echo "first='$first' last='$last' varx='$varx'"
    # do something
done

This produces:

first='Joe' last='Johnson' varx='25'
first='Sue' last='Miller' varx='27'

Note that this approach will work even if some names in the file include spaces.

Note also that the use of cat above is not necessary:

while IFS=: read first last varx
do
    echo "first='$first' last='$last' varx='$varx'"
    # do something
done <"${smryfile}"

A side benefit of removing cat, as per the above, is that any variables that you create in the loop will survive after the loop finishes.

Related Question