Hani Goc Hani Goc - 2 months ago 11
Linux Question

How to replace spaces with points at even positions in a line using linux command

I have a file where I am trying to replace the spacing at even positions with two points. What linux command Can I use to do it?

I tried the following sed command

sed 's/\s/:/2'
from replace nth occurence of string in each line of a text file But it only replaces the second occurence.




input



TEDL15_TRAINING_00018 2 0.047883321 6 0.041573718 7 0.020443868 8 0.035350464 11 0.026124746 16 0.035317142 22 0.014992127

TEDL15_TRAINING_00015 2 0.076135024 66 0.036367059 68 0.05614619 106 0.040146504 113 0.038728137





output



TEDL15_TRAINING_00018 2:0.047883321 6:0.041573718 7:0.020443868 8 0.035350464 11:0.026124746 16:0.035317142 22:0.014992127

TEDL15_TRAINING_00015 2:0.076135024 66:0.036367059 68:0.05614619 106: 0.040146504 113:0.038728137

Answer

Supposing your text is stored in a file named test.txt:

sed -e 's/ \([[:digit:]]\+\) \([[:digit:]].[[:digit:]]\+\)/ \1:\2/g' test.txt

Explanation:

Basically your input numbers have the following format (where _ represents a space):

_digit+_digit.digit+ 

So the above just look for this pattern, capture its groups and rewrite them by inserting a ":"