Converting odd values to even values(or vice-versa) located in a column

Hello All,

I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B.
Note that my data has some other columns(not shown here) too (around 100) after col2.

Tool,Data
A,1
A,3
A,5
....
so on
B,2
B,4
.... 

so on
I want to convert all odd numbers to even numbers( adding 1).. So my output is like below.

Tool,Data
A,2
A,4
A,6
.....
so on
B,2
B,4
.... 
so on

I need a one liner code for this without splitting the file into 2 portions and joining them later.
I tried tried like

awk -F, 'NR==1; $1=='A'' infile | awk -F, '$2=$2+1' >temp1.CSV
awk -F, '$1=='B'  infile >temp2.CSV
cat temp1.CSV temp2.CSV >outfile

But I want to avoid these temporary files temp*..

Thanks

Sidda

Try:

awk -F, '$2%2{$2++}1' OFS=, infile > outfile
1 Like

you can avoid temp files but not temp file.
you have to use atleast one temp file.

try

awk -F, '$1==A{$2+=1}1' OFS="," file > temp
mv temp file 
1 Like

Try this:

$ cat t
A,2
A,4
A,6
B,2
B,4

$ awk '{n = substr($0, match($0, /[0-9]+/), RLENGTH) + 1; sub(/[0-9]+/, n); print }' t
A,3
A,5
A,7
B,3
B,5
perl -i.bak -F',' -ape 'if($F[0] eq "A") {$F[1]++;$_=join(",",@F)."\n"}' file