Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

EDIT: After using cat -A myfile it seems problem that those spaces are not spaces, but M-BM- special character. Using any symbol . as Oli suggested for search is not good idea as there is risk some other characters will be removed.

So actual question is - does anyone have an idea how to remove M-BM- special character without risking losing other characters?

I found an answer: How to remove special 'M-BM-' character with sed

I have a string of text:

" . . ."

that is

space dot space dot space dot

I am trying to replace all occurence of this string in text file to

"..."

that is

dot dot dot

I was trying to do with sed:

sed -r 's:\s\.\s\.\s\.:...:g' -i sed-dots

Unfortunately, it does not change input file even a bit. File: https://www.dropbox.com/s/46zmiruy3ln85a1/sed-dots

When I try to do replace same string in text editor (I use geany) it is found and replaced properly.

Only reason I can think of is that some (or all) of those spaces are not really spaces, but some special character.

Does anyone have idea how to find and replace that string with sed (or any other command line tool)? Please test your idea on my file, as problem is not as obvious as it might seem to be - this is why I asked about it.

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

First I would start by testing with echo and piping that into sed, than using a real file. Secondly, you can use a {n} in the extended regex model to denote multiples and limits.

You were pretty much there but your regex expected a leading space.

$ echo 'cheese . . . muffins' | sed -r 's/(\s?\.){3}/ dot dot dot/g'
cheese dot dot dot muffins

Note the \s? is still greedy enough to ruin the output, so I've added a space to the output. You might not want that. I've also made the space optional, so it'll match all of the following:

...
. ..
.. .
. . .
 . . . 

Just remove the optional ? flag.


Given your problem with unicode (in the comments) you can force the data to its ASCII equivalence with iconv and then sed it:

$ iconv -f utf-8 -t ascii//translit sed-dots | sed -r 's/(\s?\.){3}/ dot dot dot/g'
Lorem ipsum dot dot dot
Some dot dot dot more text
share|improve this answer
    
I'm surprised that you recommend using echo instead of catting a file, at least when you cat a file, you know the shell is not interpreting anything, and neither is echo. –  Flimm Mar 12 '13 at 14:36
    
@Flimm for a simple example with dots, this isn't really an issue. If you're going to load from a file, don't bother with cat - just have sed load the file (per the OP's example) but don't save inline (remove -i, so you can see and test against the output). –  Oli Mar 12 '13 at 14:53
    
@Oli It works with your example, but it does not work with my file (in my question, there is a link). That is problem - your command and others should work, but they don't as there is some problem with those dots. Please test your command on my file and you will see that it does not work. –  Rafal Mar 21 '13 at 15:27
1  
@Rafal If you look at cat -A sed-dots you can see that the "spaces" between the dots are special M-BM- characters... Not sure how they crept in there but they need replacing. If you can't target them well, this works: sed -r 's/(\s\..\..\.)/ dot dot dot/ig' sed-dots –  Oli Mar 21 '13 at 16:00
    
@Oli It works. Thank you a lot! Could you explain syntax? Are you sure it does not have any side-effects and will not replace anything else? As far as I see, this RegExp will match any character after dots. However, M-BM is not one character, it's three. So how can it possibly work? –  Rafal Mar 22 '13 at 17:02
show 1 more comment

Try the following to replace all ". " to "."

sed -r 's/\. /\./g' -i sed-dots

But for ". . ." to "..."

sed -r 's/\. \. \./\.\.\./g' -i sed-dots
share|improve this answer
add comment

I could use your file when I ran it through:

tr '\240' ' ' < sed-dots.txt > sed-dots.new

This worked without a conversion step:

sed 's/[[:blank:]]\.[[:blank:]]\.[[:blank:]]\./.../g' sed-dots.txt
share|improve this answer
    
It does not work. I guess that reason is weird M-BM character that @Oli have found. –  Rafal Mar 22 '13 at 17:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.