Need help in assigning output of n commands to n variables automatically inside a for loop

Please help me to automatically assign the output of awk command to the variables cs3, cs4, cs5 and cs6 using a for loop. The below code is not working.

for i in 3 4 5 6
do
        cs$i=`awk -F"|" 'BEGIN{sum=0}{sum=sum+$'$i'}END{printf("%d\n", sum)}' css`
done
echo $cs3 $cs4 $cs5 $cs6

Didnt someone point out howto convert env variables into awk variables using the "-v" awk option...and what is the idea of using multiple single ticks here sum+$'$i'.

What shell do you have?

For that matter, what does your input data look like? Maybe there's a way to do it in pure shell instead of calling awk n times.

Hi Corona688, I have K-Shell.

What does your input data look like? Maybe there's a way to do it in pure shell instead of calling awk n times.

Hi Corona688, instead of using the following code to assign variables cs3, cs4, cs5, cs6, cs7, cs8 and cs9, I used the for loop mentioned in the thread. But, the for loop is not working. Please help.

awk -F"|" 'BEGIN{sum=0}{sum=sum+$3}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$4}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$5}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$6}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$7}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$8}END{printf("%d\n", sum)}' css
awk -F"|" 'BEGIN{sum=0}{sum=sum+$9}END{printf("%d\n", sum)}' css

The file css looks like below. I want to add the values in each column and store them in variables for each column
00:00 to 01:00|666|666|666|0|0|0|0|0|0|0|0
01:00 to 02:00|37|2|37|0|0|0|0|0|0|0|0
02:00 to 03:00|1860|1860|1860|0|0|0|0|0|0|0|0
03:00 to 04:00|892|890|890|0|0|0|0|0|0|0|0
04:00 to 05:00|1586|1588|1588|0|0|0|0|0|0|0|0
05:00 to 06:00|1912|1912|1912|0|0|0|0|0|0|0|0
06:00 to 07:00|2815|2815|2815|0|0|0|0|0|0|0|0
07:00 to 08:00|2497|2497|2497|0|0|0|0|0|0|0|0
08:00 to 09:00|3611|3611|3611|0|0|0|0|0|0|0|0
09:00 to 10:00|4903|4881|4881|0|0|0|0|0|0|0|0
10:00 to 11:00|5624|5642|5642|0|0|0|0|0|0|0|0
11:00 to 12:00|5617|5611|5611|0|0|0|0|0|0|0|0
12:00 to 13:00|7877|6505|6484|21|0|0|0|0|0|0|0
13:00 to 14:00|6608|7110|3202|2213|1511|184|0|0|0|0|0
14:00 to 15:00|5596|6469|4705|1764|0|0|0|0|0|0|0
15:00 to 16:00|5005|5003|5003|0|0|0|0|0|0|0|0
16:00 to 17:00|4220|4229|4229|0|0|0|0|0|0|0|0
17:00 to 18:00|7769|7771|7771|0|0|0|0|0|0|0|0
18:00 to 19:00|6626|6626|6626|0|0|0|0|0|0|0|0
19:00 to 20:00|4852|4852|4852|0|0|0|0|0|0|0|0
20:00 to 21:00|5758|5758|5758|0|0|0|0|0|0|0|0
21:00 to 22:00|5019|5019|5019|0|0|0|0|0|0|0|0
22:00 to 23:00|1840|1840|1840|0|0|0|0|0|0|0|0
23:00 to 24:00|159|159|159|0|0|0|0|0|0|0|0

If it's just numbers like 1|2|3|4, then

#!/bin/ksh

IFS="|"
# Read individual lines from inputfile
while read LINE
do
        # Split $LINE apart on IFS into the array CSI
        set -A CSI $LINE

        # Start at column 1.  Column 0 would be that time column
        N=1
        while [ ! -z "${CSI[$N]}" ]
        do
                # If the column is blank, set it to zero
                [ -z "${CS[$N]}" ] && ((CS[$N]=0))
                # Add the number to the column
                ((CS[$N] += CSI[$N]))
                # Add one to N
                ((N++))
        done
done < inputfile

N=1
while [ ! -z "${CS[$N]}" ]
do
        echo "cs$N=${CS[$N]}"
        ((N++))
done
$ cat >inputfile <<EOF
1|2|3|4
5|6|7|8
9|10|11|12
EOF
$ ./addcol.sh
cs1=18
cs2=21
cs3=24
$

---------- Post updated at 12:45 PM ---------- Previous update was at 12:41 PM ----------

Updated to fit your data better:

#!/bin/ksh

IFS="|"
# Read individual lines from inputfile
while read TIME LINE
do
        # Split $LINE apart on IFS into the array CSI
        set -A CSI $LINE

        N=0
        while [ ! -z "${CSI[$N]}" ]
        do
                [ -z "${CS[$N]}" ] && ((CS[$N]=0))
                ((CS[$N] += CSI[$N]))
                ((N++))
        done
done < inputfile

N=0
while [ ! -z "${CS[$N]}" ]
do
        echo "cs$N=${CS[$N]}"
        ((N++))
done

---------- Post updated at 12:48 PM ---------- Previous update was at 12:45 PM ----------

...or just do it all in awk then read it once into the shell:

LINE="`awk -v FS='|' '{ for(N=2; N<=NF; N++) { T[N]+=$N; MAX=N; } }
END { for(N=2; N<=MAX; N++) printf("%s ", T[N]); printf("\n"); }' inputfile`"

set -A CS $LINE

N=0
while [ ! -z "${CS[$N]}" ]
do
        echo "CS$N=${CS[$N]}"
        ((N++))
done

How about with a slight modification to your shell script...

for i in 3 4 5 6
do
        eval cs$i=`awk -v x=$i -F"|" '{sum+=$x} END {print sum}' css`
done
echo $cs3 $cs4 $cs5 $cs6

which runs awk 4 times to do something it could've done the first time around...

yes i shold have ready the op's earlier posts