How to use sed to replace space partial

source "PUT 810 712 0001 ILC AK4 00 0 00 00"
It needs to be changed to "PUT,810,712,0001,ILC,AK4 00 0 00 00"

Thanks in advance.

quick reply:

If you have a strings of similar types, then you can use something like:

str=`echo "PUT 810 712 0001 ILC AK4 00 0 00 00" | sed 's/[ ]/,/5'`

echo $str

It will replace first 5 occurrences of space from left to right order!.

Thanks your reply.

sed 's/[ ]/,/5' is to replace the fifth space with comma.

the result is "PUT 810 712 0001 ILC,AK4 00 0 00 00" and not the expected one.

 
str=$(echo "PUT 810 712 0001 ILC AK4 00 0 00 00" | sed 's/^\([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\)\(.*\)/\1,\2\,\3\,4\,\5,\6\7/')

Here assumption is that 1st 6 WORDS are separated by a single space and you need to replace those 1st 5 spaces with a comma. If this is not you want, command has to be modified accordingly.

I think this will work out for you, if the strings are of similar type( its just a workaround, as you must expect a much better solution from forum folks!):

str=`echo "PUT 810 712 0001 ILC AK4 00 0 00 00" | awk -F " " '{print $1","$2","$3","$4","$5","$6,$7,$8,$9,$10}'`

echo $str

@IND123, will work if string has only 10 fields always.

change the number of iterations according to the commas required..

str="PUT 810 712 0001 ILC AK4 00 0 00 00"
for i in 1 2 4 5 6
do
        str=$(echo "$str" | sed 's/ /,/')
done

echo $str