Awk command in while loop

Hello!

I've got a loop in which I am processing a list of values gotten through a file with read command.

It seems that instead of processing the lines (values) one by one, I process them all together.

the input file is:

 
20
20
20
80
70
70
20 

The code is:

 
#!/bin/sh
 
while read -r Line
do
        STR5="$(awk 'BEGIN {FS = ","} ; {print $1}')"
        if [ "$STR5" = "20" ]; then
                echo "CHOICE A" >> SEN5$1.txt
        elif [ "$STR5" = "70" ]; then
                echo "CHOICE B" >> SEN5$1.txt
        elif [ "$STR5" = "80" ]; then
                echo "CHOICE C" >> SEN5$1.txt
        else
                echo "$STR5" >> SEN5$1.txt
        fi
        echo "$STR5" >> SEN5$1.txt
done < input.txt

and produces the same output as the input file, meaning that the last 'else' statement is validated each time. By testing I have seen that it takes the whole file (all lines) in each iteration.

I have also tested this code:

 
while read -r Line
do
awk 'BEGIN {FS = ","} ; { if ($1 = 20) print "CHOICE A"; else if ($1 = 70) print "CHOICE B"; else if ($1 = 80) print "CHOICE C" ; else print $1}' >> SEN5$1.txt
done < input.txt

which returns

 
CHOICE A
CHOICE A
CHOICE A
CHOICE A
CHOICE A
CHOICE A
CHOICE A

which propably is due to checking with the 1st value each time.

Any ideas?

How about ...

#!/bin/bash

cat in.file | while read LINE
do
  case $LINE in
    "20") echo "Choice A" >> out.file ;;
    "70") echo "Choice B" >> out.file ;;
    "80") echo "Choice C" >> out.file ;;
    *   ) echo "Crapshot" >> out.file ;;
  esac
done

exit 0
#finis
[house@leonov] bash test.bash
[house@leonov] cat out.file
Choice A
Choice A
Choice A
Choice C
Choice B
Choice B
Choice A

Doh!

Using the $Line itself, dunno why it escaped me this easily! :embarassed:

Thanks a lot Dr :wink: