Search duplicate field and replace one of them with new value

Dear All,

I have file with 4 columns:

1 AA 0 21
2 BB 0 31
3 AA 0 21
4 CC 0 41

I would like to find the duplicate record based on column 2 and replace the 4th column of the duplicate by a new value. So, the output will be:

1 AA 0 21
2 BB 0 31
3 AA 0 -21
4 CC 0 41

Any suggestions will be highly appreciated.

Thanks,
Ezhil

What new value?

Try this,

nawk '{if(a[$2] !~ 1){a[$2] = 1}else{$4=-$4}}1' txt

Regards,
Indu

# awk '{if(a[$2]++)$NF=-$NF}1' file
1 AA 0 21
2 BB 0 31
3 AA 0 -21
4 CC 0 41