How to change last character in string with a variable value.

Hey Guys,

I have text such as this.

28003,ALCORN,2
28009,BENTON,2
28013,CALHOUN,2
28017,CHICKASAW,2
47017,CARROLL,2
05021,CLAY,0

The last digit after the final "," is a variable value. This is the base file. I have to do further execution on this file later and I need to update the 3rd value at that time. So, How do I open this file, and append the last digit to my new variable, and also save as the same filename. I have tried multiple unsuccessful options.

An example of what I want is essentially the same output only with the new variable values appended. Using shell by the way.

awk -F, '{$3=$3"new value"}1' file

Change red "new value" with whatever you want to append there.

Can it be a variable? Like $x

awk -F, -vx=$x '{$3=$3""x}1' file

OK. Does the x in -vx represent the variable x? Is the $x the only time the variable is referenced?

Also. How would I do this in a loop. The script has to parse through the file appending if necessary, and not appending if not necessary. Obviously I'll need a "for" loop and an "if" statement. Just not sure how to set it up. Do I need to set your code to a variable itself?

Something such as
for.......
if argument is true
#append the third column in the file on this line only
else
#do nothing go to next line.
fi
done

-vx <- this "x" is awk's variable, while $x is shell's variable. You don't need shell's loop to go through all the lines. It can be done in awk. Just let me know what is the condition for appending.

There's so many way to scripting but I am not really sure if it is worth it.,,if ever that would be working if i use all the shell scripts in the same time..
My friend sharing me to all his information bout shell script but does not working don't know why i think it must be at the right shell programming before to function.. guys do you have any information regarding in this case if possible

Thanks for considerate:))

Ok. I'm just gonna lay it out there for ya. I'm not the best coder (obviously but here is what I have. Code is below.

#!/bin/sh
n=$(cat $fipslist | wc -l)
i=1
for ((i; i<n+1; i++))
do
  fipscode=$(cat $fipslist | head -$i | tail -1 | cut -c 17-21)
  countyname=$( cat $fipslist | grep $fipscode | cut -c 1-16 | tr -d " ")
  fipschecker=$(cat $cancountieslist | grep $fipscode" ")

  if [ "$fipschecker" != "" ]; then
    #echo $fipscode","$countyname","$threatcodecan >> $wcnoutput
    #HERE IS WHERE I NEED YOUR CODE.  RIGHT NOW IT JUST OVERWRITES $WCNOUTPUT.  BASED ON THE CONDITION ABOVE IN THE IF   STATEMENT, IF TRUE, I NEED IT TO JUST OPEN $WCNOUPUT, AND APPEND $THREATCODECAN (WHICH I HAVE ASSIGNED ELSEWHERE) TO $WCNOUTPUT IN THE THIRD COLUMN OF THE FILE (WHICH IS A CSV FILE).  LEAVING ALL OTHER LINES IN $WCNOUTPUT UNCHANGED.
  fi
done