Return Awk Variable to Shell

I'm a bit stuck in getting variable from awk to shell. I tried searching but most of them showing to assign to shell variable via..

VAR=`echo $line | awk -F: '{print $1}'`

which is correct ofcourse

My problem is multiple assignments of variable like this one. The above solution will give me several lines of same coding.

 echo $line | awk -F: '{account=$1;schedule=$2;unixlist=$3;ntlist=$4;urllist=$5;pinglist=$6;ntpassfile=$7;}'

Here's the $LINE

CLIENT:0,3,6,9,12,15,18,21:/home/erm/user/CLIENT/PARM/unix_servers.txt:/home/erm/user/CLIENT/PARM/nt_servers.txt:/home/erm/user/CLIENT/PARM/CLIENT_url.txt:/home/erm/user/COACH/PARM/ping_serv_list.txt:/home/erm/user/COACH/PARM/pass.txt

Is it ok for you to handle them in an shell array or do you want to split them to different shell variables?

well i would want to them to be in different shell variables, but anyway can you show me both how to work on this?

Arrays depend on the shell you use, there should be some threads in the forum here you can find via the search function.

You could also simply use one line per variable definition like this, without needing awk:

ACCOUNT=`echo $line | cut -d":" -f1`
SCHEDULE=`echo $line | cut -d":" -f2`
...

yes.. that's the current code i'm using.. can i not do it in one line? or at least the shortest way

I think this is the most simple and most clear. If you go by awk and maybe print out the variable names as text together with the = and the value, you'll have to eval the output so it will be recognized as shell variables.
I would stick with the crude solution. There is no other solution coming to my mind that would be shorter. Maybe another reader has an idea.

Another way ...

$ line="a:b:c"
$ echo $line | IFS=":" read var1 var2 var3
$ echo $var1
a
$ echo $var2
b
$ echo $var3
c

Two solutions:

  1. using eval
  2. using IFS, which fpmurphy also demonstrated
#! /bin/sh

LINE="CLIENT:0,3,6,9,12,15,18,21:/home/erm/user/CLIENT/PARM/unix_servers.txt:/home/erm/user/CLIENT/PARM/nt_servers.txt:/home/erm/user/CLIENT/PARM/CLIENT_url.txt:/home/erm/user/COACH/PARM/ping_serv_list.txt:/home/erm/user/COACH/PARM/pass.txt"

#
# solution 1 - two passes using eval
#
eval `echo $LINE | awk -F: '{printf("account=\"%s\";schedule=\"%s\";unixlist=\"%s\";ntlist=\"%s\";urllist=\"%s\";pinglist=\"%s\";ntpassfile=\"%s\";",$1,$2,$3,$4,$5,$6,$7)}'`
echo "SOLUTION-1:"
echo "account=$account"
echo "schedule=$schedule"
echo "unixlist=$unixlist"
echo "ntlist=$ntlist"
echo "urllist=$urllist"
echo "pinglist=$pinglist"
echo "ntpassfile=$ntpassfile"

#
# solution 2 - set IFS to be colon to help filtering
#
IFS_original=$IFS
IFS=":"
set -- $LINE
echo "SOLUTION-2:"
echo "account=$1"
echo "schedule=$2"
echo "unixlist=$3"
echo "ntlist=$4"
echo "urllist=$5"
echo "pinglist=$6"
echo "ntpassfile=$7"
IFS=$IFS_original
oifs="$IFS"
line="a:b:c"

# only ksh
echo $line | IFS=":" read v1 v2 v3 vx
echo "A v1:$v1, v2:$v2"

# ksh+bash:
IFS=":" read v1 v2 v3 vx <<EOF
$line
EOF
echo "C v1:$v1, v2:$v2"

# ksh+bash:
IFS=":"
array=( $line )
IFS="$oifs"
echo "D v1:${array[0]}, v2:${array[1]}"

And all previous ...

In ksh you can use also set to array (compare chihung set), save current IFS, set IFS=:" and return IFS. Values are in the array, starting index 0.

set -A array -- $line

Using awk to parse only one line, awk is slowest, but if whole script has done using awk, awk is much faster as ksh/bash/sh script.

thank you everyone.. got it, happy scripting.

IFS=: comps=($line)

will do it.