bashshellunixaixsunos

How to replace comma with character in Unix bash


I have the below string:

"BBBB,AAAA" seperated with comma. I need to append .done with each value of the string.

When i try below. It doesnot work. I try to replace , with .done but its doesnot work. Also i need the .done at the end of both AAAA and BBBB

-bash-3.2$ echo "AAAA,BBBB" |tr "," ".done" 
 AAAA.BBBB

Expected Output:

AAAA.done  BBBB.done

Thanks for your help.

Edit:

After @Ravindra Suggestion, i modified it little bit and its working fine but the problem now is to trim the spaces.

-bash-3.2$ echo "AAAA,BBBB ,CCC, DDD" | sed 's/,/.done /g;s/$/.done/'
 AAAA.done BBBB .done  CCC.done  DDD.done
               ^  
               getting space here

OS name: SunOS


Solution

  • Following simple sed may help you on same.

    val="AAAA,BBBB"
    echo "$val" | sed 's/,/.done /;s/$/.done/'
    

    Output will be as follows.

    AAAA.done BBBB.done
    

    EDIT:

    awk -v val="$val" 'BEGIN{gsub(/,| ,/,".done ",val);sub(/$/,".done",val);print val}'
    

    EDIT2:

    awk -v val="$val" 'BEGIN{gsub(/,| ,/,".done ",val);sub(/$/,".done",val);print val}'
    AAA.done  BBB.done  CCC.done DDD.done