This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how to replace dot as 'Missing' value for all columns using sed

I tried to replace all the dot as missing value. I tried used sed 's/^./Missing/g' test.txt. There is no change. When used sed 's/.$/Missing/g' test.txt. Only the dot in the last column changed.

ex. col1 col2 col3 . 13.34 12.44 12.3 . 12.22 . 125.5 .

sed linux
sed 's/ \./ Missing/g' test.txt

3 answers

Assuming your data is space separated:

sed 's/ . / Missing /g' test.txt

It's not working. I just tried 'sed 's/./Missing/g' colon.txt', it replaced all the dot, even the dot in '12.44'

Note the spaces before and after the dot and before and after the Missing

Thanks to your advice. I tried the following, it works. sed 's/.\t/Missing\t/g' test.txt

Is your data space separated? Or tab? What Operating system? You could try instead (spaces inbetween brackets):

sed 's/[ ].[ ]/ Missing /g' test.txt.

to be really space/tab independent, the proper way to write it would be

sed 's/\(\s\)\.\(\s\)/\1Missing\2/g' test.txt

unfortunately this won't work with 2 consecutive dot columns, neither with the first and last columns. see my answer below.

a sed independent solution on field separators, that would deal with 2 consecutive dot columns plus the first and last columns would be the following

sed 's/\./Missing/g; s/Missing\(\S\)/.\1/g' test.txt

or its perl alternative

perl -pe 's/\./Missing/g; s/Missing(\S)/.$1/g' test.txt
sed 's/\./Missing/g' test.txt

That will also adapt the decimal separator.

Log in to answer this question.