how to replace dot as 'Missing' value for all columns using sed
3
0
Entering edit mode
8.0 years ago
newbiebio ▴ 80

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 • 2.2k views
ADD COMMENT
0
Entering edit mode
sed 's/ \./ Missing/g' test.txt
ADD REPLY
2
Entering edit mode
8.0 years ago
Tonor ▴ 480

Assuming your data is space separated:

sed 's/ . / Missing /g' test.txt
ADD COMMENT
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

sed 's/[ ].[ ]/ Missing /g' test.txt.
ADD REPLY
1
Entering edit mode
8.0 years ago

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
ADD COMMENT
0
Entering edit mode
8.0 years ago
sed 's/\./Missing/g' test.txt
ADD COMMENT
0
Entering edit mode

That will also adapt the decimal separator.

ADD REPLY

Login before adding your answer.

Traffic: 1920 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6