1

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

1
  • Next time it might be nice to state that "Does Not Work" means that only the comma was being replaced by the 1st character in ".done" i.e the dot. Commented Mar 22, 2018 at 7:27

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

14 Comments

Sorry it doesnot work incase val='AAA,BBB,CCC". I mean when the number of parameters are increased. It doesnot work. Can you please suggest
@XING, check my edit solution now and let me know on same then. It should handle multiple , in it too, let me know if this works well for you?
One more thing ..incase the string is like "AAA, BBB , CCC ,DDD" , here in this case the result is AAA.done BBB .done CCC .done. i mean the space between the BBB .done should b removed as well
@XING, I have edited my solution now, check and let me know then?
Getting syntax error awk: syntax error near line 1 awk: bailing out near line 1 . can you please check
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.