0

I'm trying to replace <92> with single quote in a webpage using tr. When I do

tr "<92>" "'" < index.html

the output shows me that the replacement has occurred. So to edit in place (or as close as tr gets) I do

tr "<92>" "'" < index.html >> index.html

The file is unchanged. For reference, redirecting to another file altogether doesn't work correctly either.

  • 1
    what happens when you `>> index_new.html`? You should *never* in- and output to the same file – Alexander Apr 07 '15 at 16:39
  • `tr` *translates* characters, not words. Your original command replaces `<` with `'`, and ignores `9`, `2`, and `>`. You'll need to use something like `sed` to do something more complex than character translations. – Mr. Llama Apr 07 '15 at 17:04
  • You are 100% barking up the wrong tree with `tr` here. It replaces 1 or more CHARACTERS by 1 or more CHARACTERS not sequences. Use `sed` for this. – Mark Setchell Apr 07 '15 at 20:08

1 Answers1

0

As far as I know, tr can't be used for inplace modification. You could use sed as an alternative. Try:

sed "s/<92>/'/g" index.html

If you are agree with the output, use the inplace flag (-i)

sed -i "s/<92>/'/g" index.html

You can also refer to this answer: tr command not able to direct the Output?

Community
  • 1
  • 1
ohe
  • 3,461
  • 3
  • 26
  • 50
  • I actually had tried sed first as that was what I knew for this kind of job but that precise command: 212 sed -i "s/<92>/'/g" index.html didn't work there either. Which led me to tr. Depending on which man pages you believe tr can write to the same file it's translating as long as you redirect with append standard output (>>) instead of overwrite standard output (>). Overwrite results in the contents of the original file being clobbered. I'm starting to wonder about the environment this provider has me working in. I'm going to try in another place. – Jim Pollard Apr 07 '15 at 18:14
  • Ok, the first string is a meta tag which nothing but vi seems to see. Any recommendations for a stream editor that can handle meta tags? – Jim Pollard Apr 07 '15 at 20:05
  • Oh, in that case, I guess you want to replace smart quotes (these characters : `‘’`). Well, in that case, I think `sed -i "s/[‘’]/'/g"`will work. – ohe Apr 07 '15 at 20:23
  • Thanks! That would be just the ticket except that the <92> only shows up in vim (as hex). I'm closing this thread to go find a way to batch convert CP1252 files to UTF-8. – Jim Pollard Apr 07 '15 at 21:00