Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to convert all apostrophes in this file to X:

Bob's book
Bob’s book
Bob′s book  # (Might look the same but actually different)

The first apostrophe is replaced as expected:

$ cat file | tr "'" "X"
BobXs book
Bob’s book
Bob′s book

But the the two other kinds of apostrophe, strange things happen:

$ cat file | tr "’" "X"
Bob's book
BobXXXs book
BobXX�s book

$ cat file | tr "′" "X"
Bob's book
BobXX�s book
BobXXXs book

How to make it work?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

tr works in units of bytes, which means it doesn't work properly for multi-byte encodings like UTF-8. The only solutions I know of are to find a version of tr that supports Unicode, or switch to sed or some other tool that can do string replacement.

share|improve this answer
2  
sed -e "s/’/X/" works. –  Nicolas Raoul Sep 19 '12 at 3:07

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.