How to replace the 10th column?

how to replace the 10th colum? Each line begins similarly, but they all ends variously.

Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,24,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,8,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,24,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,24,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,8,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,0,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,0,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,0,0,1,1,347963352,1,1,

anyway, I want the above text file to look like this:
if field 10 = 24 replace witch Keyword1
if field 10 = 8 replace witch Keyword2
if field 10 = 0 replace witch Keyword3

Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,Keyword1,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,Keyword2,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,Keyword1,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,Keyword1,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,Keyword2,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,Keyword3,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,Keyword3,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,Keyword3,0,1,1,347963352,1,1,

Any ideas? I searched and found extensive sed/awk tricks..but this (I think) is simpler.

Hi, try:

awk -F, 'BEGIN{OFS=FS;A[0]="keyword3";A[8]="keyword2";A[24]="keyword1"};A[$10] ? $10=A[$10] : 1' input.txt

Regards.

1 Like

Try:

awk '$10==24{$10="keyword1"} $10==8{$10="keyword2"} $10==0{$10="keyword3"}1' FS=, OFS=, file
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,keyword1,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,keyword2,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,keyword1,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,keyword1,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,keyword2,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,keyword3,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,keyword3,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,keyword3,0,1,1,347963352,1,1,
1 Like

awk -f oo.awk myFile , where oo.awk is:

BEGIN {
  FS=OFS=","
  f2r=10
  split("24,8,0",fA,FS)
  split("Keyword1,Keyword2,Keyword3", fAr,FS)
}
FNR>1{
  for(i=1; i in fA;i++)
    if ($f2r == fA) {
      $f2r=fAr
      break
   }
}
1
1 Like

Here, it should not be value "keyword1" is equal to "8" by example...

Regards.

1 Like

I don't know what you mean Disedorgue. My script does not compare "keyword1" to "8"
I added the output of the command in my post, which is what the OP required.

1 Like

Imagine that the keyword1 is "8" , keyword2 is "5" and keyword3 is "6":

$ awk '$10==24{$10="8"} $10==8{$10="5"} $10==0{$10="6"}1' FS=, OFS=, input.txt 
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,5,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,5,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,5,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,5,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,5,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,6,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,6,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,6,0,1,1,347963352,1,1,
$ awk -F, 'BEGIN{OFS=FS;A[0]="6";A[8]="5";A[24]="8"};A[$10] ? $10=A[$10] : 1' input.txt 
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17
2015/03/14 15:00:04,2015/03/14 15:00:04,0,Server05,Omega-God,Beta_G,101011247,1,1,8,7,0,,339083645,1,1,
2015/03/14 15:13:48,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,5,1,5,7,0,,319207661,1,1,103510841
2015/03/14 15:14:55,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,6,1,8,7,0,,319207661,1,1,
2015/03/14 15:29:56,2015/03/14 09:40:38,0,Server03,Omega-God,Beta_G,101011247,7,1,8,7,0,,319207661,1,1,
2015/03/14 15:50:53,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,8,1,5,7,0,,319207661,1,1,103510841
2015/03/14 18:50:58,2015/03/14 09:40:38,0,Server04,Omega-God,Beta_G,101011247,12,0,6,0,1,1,319207661,1,1,103510841
2015/03/14 18:51:03,2015/03/14 09:40:38,1,Server01,101011247,Server04,Omega-God,1,0,6,0,1,1,347955199,1,1,
2015/03/14 18:51:11,2015/03/14 15:05:19,1,Server04,101011247,Server04,Omega-God,1,0,6,0,1,1,347963352,1,1,

Regards.

1 Like

Yes, I see what you mean, if there is a possibility that the keyword can be equal to the value, then that will influence the result. So it should only be used if that cannot happen, as appears to be the case with the user's input.

But Good observation, thanks...

Then it is better if we use something like this:

awk '{if($10==24) $10="8"; else if ($10==8) $10="5"; else if($10==0) $10="6"}1' FS=, OFS=, file
1 Like

Or:

awk '{$10 = ($10 == 24) ? "Keyword1" : ($10 == 8) ? "Keyword2" : ($10 == 0) ? "Keyword3" : $10}1'  FS=, OFS=, file

Note that disedorgue was correct in noting that Scrutinizer's suggestion would fail if one of the keywords was one of the input values being replaced, but disedorgue's suggestion:

awk -F, 'BEGIN{OFS=FS;A[0]="keyword3";A[8]="keyword2";A[24]="keyword1"};A[$10] ? $10=A[$10] : 1' input.txt

will also fail if one of the keywords is 0 or an empty string.

1 Like

Exactly, thanks Don Cragun.
A possible fix:

awk -F, 'BEGIN{OFS=FS;A[0]="keyword3";A[8]="keyword2";A[24]="keyword1"};($10 in A) ? ($10=A[$10])1 : 1' input.txt

Regards.