Replace last occurrence of a character in a field with awk

前端 未结 2 1858
抹茶落季
抹茶落季 2021-01-13 23:13

I\'m trying to replace the last occurrence of a character in a field with awk. Given is a file like this one:

John,Doe,Abc fgh 123,Abc
John,Doe,Ijk-nop 45D,D         


        
2条回答
  •  遥遥无期
    2021-01-13 23:56

    Here is a short awk

    awk '{$NF=RS$NF;sub(" "RS,",")}1' file
    John,Doe,Abc fgh,123,Abc
    John,Doe,Ijk-nop,45D,Def
    John,Doe,Qr s Uvw,6,Ghi
    

    Updated due to Eds comment.

    Or you can use the rev tools.

    rev file | sed 's/ /,/' | rev
    John,Doe,Abc fgh,123,Abc
    John,Doe,Ijk-nop,45D,Def
    John,Doe,Qr s Uvw,6,Ghi
    

    Revers the line, then replace first space with ,, then revers again.

提交回复
热议问题