I'm needing help with assigning variables inside a while loop of ksh script.
I have an input text file and ksh script below and I'm trying to create a script which will read the input file line by line, assign first and second word to variables and process the variables according to the contents.
$ more file.txt
AAA 001
BBB 002
CCC 003
DDD 004
$ more looptest.ksh
#! /bin/ksh
while read line
do
awk '{print FS,$1}' >> var1
awk '{print FS,$2}' >> var2
if [[ $var1 = AAA ]]
then
echo group1 $var2
elif [[ $var1 = CCC ]]
then
echo group2 $var2
fi
done < file.txt
The desired output for my ksh script is the below but the section where I assign first and second word to variables isn't working. Could someone tell me how to fix this?
You are redirecting the output to a file and not to a variable. And nowhere you are using the variable "line" in the awk statement. Change the above 2 lines as below and try again.