Match a Pattern & Replace The value Using AWK

I have a csv file in which i have to search a particular string and replace the data in any column with something else. How do i do it using awk.

file
------
2001,John,USA,MN,20101001,29091.50,M,Active,Y
2002,Mike,USA,NY,20090130,342.00,M,Pending,N
2003,Steffi,USA,MN,20070101,23222.89,F,Active,Y

Now if i want to search this file for data in column 4, i can say:

awk -F, '$4 == "MN" {print $1}' file

This would give me the first column from all rows in the file that have MN in 4th column.

But how do i replace the data in say 8th column? e.g Change it to N if 4th column has MN in it.

Thanks.

Does it have to be awk?

perl -F"," -lane '$F[8]="N" if ($F[3]=="MN"); print join (",",@F)' file.txt

---------- Post updated at 15:03 ---------- Previous update was at 14:58 ----------

My first attempt at awk:

awk 'BEGIN{FS=",";OFS=","} ($4 == "MN") {$9="N"}1' file.txt
1 Like

great job..it worked :b:

awk '$4=="MN" {$NF="N"}1' FS=, OFS=, infile