Simple awk use

Hi,

I have a file such that:

40454,31,48,4,1304.967741935484,1
31708,25,48,4,1268.32,1
20900,64501,671,788,0.3240259840932699,0
20137,51358,834,743,0.3920908135051988,0

I want to replace the 6th column by "ones" if it is 1, and with "zeros" if it is 0.

Thanks.

awk -F, '{ $6=($6==1)?"ones":$6; $6=($6==0)?"zeros":$6; }1' OFS=, filename

What have you tried so far?

bakunin

Thanks.

I know how to replace a specific column in all lines, but could not figure out how to match and replace.

Hey,

Try this,

Little bit modification @ bipinajith's post.

awk 'BEGIN{FS=OFS=",";}{$6=($6==1)?"ones":($6==0)?"zeros":$6;}1' file

Cheers,
Ranga:)

Different approach:

awk 'BEGIN{A[0]="zeroes"; A[1]="ones"} $6 in A{$6=A[$6]}1' FS=, OFS=, file

This would rely on $6 being just 0 or 1 dependably:

$ awk '{$6=($6?"ones":"zeroes")}1' FS=, OFS=, file