Input a variable and write to a file using awk

Hi

I am trying to edit a csv file. Bacically I need to input a search variable and the value that must be changed in one of the fields corresponding to that searched variable.

My csv file looks like so:

1,1A,5
1,1B,2
1,1C,3
2,2A,7
2,2B,4
2,2C,0
3,3A,1
3,3B,6
3,3C,4

I want to input a value for the second field (these are unique) and input a new value for the third field.

Example, If I input 1A and 2, then the new value of 2 must be written to the file and replace 5.

This is my awk file:

BEGIN {
FS=","
}
{
if (cellid == $2)
print "old parameter value was: " $3 " " $2 " new value is now " newvalue;
} print $3=newvalue>ds_pl_cell1.csv; 

And from my command line, I call this:

 awk -v "cellid=1A" -v "newvalue=0" -f changefile.awk ds_pl_cell.csv >ds_pl_cell1.csv

Please can you help as I am very new to this.

Thank you.

Try this:

awk -F, -v cellid="1A" -v newvalue="0" '$2==cellid{$3=newvalue}1' OFS="," file > newfile

Thank you so much, this really helps.
A quick question though, I would like to use an awk file, so I can build up on it as I learn how to alter the csv file. Can you tell me how this can be done.
Thank you

You could place this in a shell script:

awk -F, -v cellid="$1" -v newvalue="$2" '$2==cellid{$3=newvalue}1' OFS="," file > newfile

and call it as:

./scriptname 1A 0

thank u very much. this works like a charm. I appreciate your help.