Code:
1434455221 - 15.12 25.13 85221 65221
1111234222 - 15.13 25.13 94122 64122
1438785223 15.14 25.14 85223 65223
1116652224 15.15 25.15 94124 64124
1116652225 - 0 25.16 94125 64125
1116652226 0 0 94126 64126
to iterate through each row I uses shell variables. But I could not compare the scond column "-" within awk.So I need to pass the shell variables inside awk using as comma separted variables with -F option. To filter 6 cases, I used 6 counter shell variables. But that is getting reset for each iteration. So I need to use static variables using -V option.
If I use all the variables as -v opion , everything is fine but the row is not iterating, that is the coumn values are also treated as static. If I use -F option for all the variables, the counters are not getting reset for each iteration, ie not static. So I need to use -F option for shell variables and -v option for static variables. If there is a work around, please let me know.
Please see the code with -F option and let me know if you want the code that I tried with other options.
#!/bin/bash -x
statfile=test.csv
case1=0
case2=0
case3=0
case4=0
case5=0
case6=0
cat $statfile | while read line
do
COLUMN1=$(echo $line | awk -F, '{print $1'})
COLUMN2=$(echo $line | awk -F, '{print $2'})
COLUMN3=$(echo $line | awk -F, '{print $3'})
COLUMN4=$(echo $line | awk -F, '{print $4'})
COLUMN5=$(echo $line | awk -F, '{print $5'})
COLUMN6=$(echo $line | awk -F, '{print $6'})
COLUMN7=$(echo $line | awk -F, '{print $7'})
newline=$(echo $COLUMN1,$COLUMN2,$COLUMN3,$COLUMN4,$COLUMN5,$COLUMN6,$COLUMN7,$case1,$case2,$case3,$case4,$case5,$case6 | awk -F, '{
if ( $3 > 0.00 ) {
if ( $2 != "-" ){
if ( $8 == 0 ){
if ( $5 > 0.00 ){
printf("%.0f,",$1)
printf("%c,",$2)
printf("%.2f,",$3)
printf("%c,",$4)
printf("%.2f,",$5)
printf("%s,",$6)
printf("%s,",$7)
$8=($8+1)
printf("%s,","case1")
printf("%d\n",$8)
}
}
else if ( $8 != 0 ){
if ( $5 == 0.00 ) {
printf("%.0f,",$1)
printf("%c,",$2)
printf("%.2f,",$3)
printf("%c,",$4)
printf("%.2f,",$5)
printf("%s,",$6)
printf("%s,",$7)
$10=($10+1)
printf("%s,","case3")
printf("%d\n",$10)
}
}
}
else if ( $2 == "-" ){
if ( $9 == 0 ){
if ( $5 > 0.00 ){
printf("%.0f,",$1)
printf("%c,",$2)
printf("%.2f,",$3)
printf("%c,",$4)
printf("%.2f,",$5)
printf("%s,",$6)
printf("%s,",$7)
$9++
printf("%s,","case2")
printf("%d\n",$9)
}
}
else if ( $11 == 0 )
{
if ( $5 == 0.00 ){
printf("%.0f,",$1)
printf("%c,",$2)
printf("%.2f,",$3)
printf("%c,",$4)
printf("%.2f,",$5)
printf("%s,",$6)
printf("%s,",$7)
$11++
printf("%s,","case4")
printf("%d\n",$11)
}
}
}
}
else if ( $3 == 0.000 ){
if ( $12 == 0 ){
if ( $5 > 0.00 ){
printf("%.0f,",$1)
printf("%c,",$2)
printf("%.2f,",$3)
printf("%c,",$4)
printf("%.2f,",$5)
printf("%s,",$6)
printf("%s,",$7)
$12++
printf("%s,","case5")
printf("%d\n",$12)
}
}
else if ( $13 == 0 ){
if ( $5 == 0.00 ){
printf("%.0f,",$1)
printf("%c,",$2)
printf("%.2f,",$3)
printf("%c,",$4)
printf("%.2f,",$5)
printf("%s,",$6)
printf("%s,",$7)
$13++
printf("%s,","case6")
printf("%d",$13)
}
}
}
}')
echo $newline
done