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

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
XING
  • 9,608
  • 4
  • 22
  • 38
  • 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. – TimBrownlaw Mar 22 '18 at 07:27

1 Answers1

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 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 Mar 26 '18 at 10:14
  • @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? – RavinderSingh13 Mar 26 '18 at 10:31
  • 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 Mar 26 '18 at 10:37
  • @XING, I have edited my solution now, check and let me know then? – RavinderSingh13 Mar 26 '18 at 10:40
  • 1
    Getting syntax error `awk: syntax error near line 1 awk: bailing out near line 1` . can you please check – XING Mar 26 '18 at 11:20
  • @XING, on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk – RavinderSingh13 Mar 26 '18 at 11:46
  • @XING, always add your OS name what you are using so that we could add solution as per that itself. – RavinderSingh13 Mar 26 '18 at 11:50
  • I mean i didnot your `awk` command rather used the previous command. But i landed up another problem. Please see if you could help. I would surely accept the answer if it work for me.. – XING Mar 26 '18 at 12:34
  • @XING, if you keep saying you have problems without giving description, I am afraid then no one could help you either, I think I have given lot of effort here but you didn't bother to provide all information in single comment or post, I leave up to you on providing information. I would suggest better to open a new thread with FULL description, thanks and keep learning !! – RavinderSingh13 Mar 26 '18 at 12:36
  • What else information you need. I already edited my question and posted OS name. Please see edit part of my question. Let me know if you dont understand anything. – XING Mar 26 '18 at 12:38
  • `awk` is somhow not working with me. Cannot i do it with some simpler way as i did ? – XING Mar 26 '18 at 12:40
  • @XING, my EDIT code was providing the required output, please see mine EDIT2 seems it is providing correct output as per your need, check and let me know then. – RavinderSingh13 Mar 26 '18 at 12:40
  • @XING, also mentioned in comment too, change awk to on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk – RavinderSingh13 Mar 26 '18 at 12:41
  • Thanks now its working. But is there a way to avoid `awk` ? – XING Mar 26 '18 at 12:45