2

The system in question is a Raspberry Pi running current Raspbian "buster".

One can get the CPU Temperature using the following:

$ vcgencmd measure_temp
temp=53.0'C

The ' may be replaced with ° using sed:

$ vcgencmd measure_temp | sed "s/'/°/"
temp=52.0°C

But I'd prefer to do this using tr which seems a "lighter-weight" alternative to sed.

I've tried the following in tr:

$ vcgencmd measure_temp | tr ' °            # nope
$ vcgencmd measure_temp | tr \' \°          # nope
temp=53.0�C 

# Yet, this works:

$ vcgencmd measure_temp | tr \' d
temp=52.0dC
$ 

What am I missing? does the degree symbol require special care?

Seamus
  • 2,522
  • 1
  • 16
  • 31
  • 4
    Possibly related: [How to make tr aware of non-ascii(unicode) characters?](https://unix.stackexchange.com/questions/228558/how-to-make-tr-aware-of-non-asciiunicode-characters) – steeldriver Apr 30 '20 at 20:47
  • @steeldriver: It seems to be related, but that's nearly 5 years ago. Hoping Gnu's got an update? Also - a bash "built-in" would work as well as `tr`, but I'm out of my depth on that - [this baffles me](http://tldp.org/LDP/abs/html/string-manipulation.html). – Seamus Apr 30 '20 at 22:25
  • 1
    The [current Coreutils documentation](https://www.gnu.org/software/coreutils/manual/html_node/tr-invocation.html#tr-invocation) still states that _"Currently `tr` fully supports only single-byte characters"_. – fra-san Apr 30 '20 at 22:39

1 Answers1

2

If you quote the degree character in tr it should work:

$ vcgencmd measure_temp | tr \' '°'

EDIT: I've just tested this and tr is indeed a bit odd...

$ echo "asdf'" | tr \' '°'
asdf�

$ TEST="asdf'"; echo ${TEST/\'/°}
asdf°

Please note: the above shell string manipulation may not work in all shell interpreters. I know it works in bash, @fra-san mentioned it works at least under ksh93, mksh, yash, zsh. So it's more portable than I initially thought.

Finally, as per @fra-san 's comment above, the current Coreutils documentation still states that "Currently tr fully supports only single-byte characters".

Pedro
  • 1,821
  • 12
  • 23
  • Here's what I get: `vcgencmd measure_temp | tr \' '°'` ==> `temp=54.0�C` – Seamus May 01 '20 at 02:59
  • & for the EDIT: `$ echo ${vcgencmd measure_temp/\'/°} **\n** -bash: ${vcgencmd measure_temp/\'/°}: bad substitution **\n** $ TEST=$(vcgencmd measure_temp); echo ${TEST/\'/°} **\n** temp=54.0°C` Any ideas why the first one failed? Or how to do this in a single command? – Seamus May 01 '20 at 03:13
  • The code you want to use is slightly different. What's between ${} is a variable name, not where you leave your command. So this would work: `OUTPUT=$(vcgencmd measure_temp); echo ${OUTPUT/\'/°};` – Pedro May 01 '20 at 14:40
  • 1
    Yes - that works - and thank you for the explanation - but it takes two commands. And yes, I realize my question didn't specify a single command, but I did ask about doing this with `tr`. I've up-voted your answer, but it's not quite `THE` answer I was hoping for. However, based on @fra-san's comment, this may not be possible with `tr`. All of that said, if you incorporate that comment in your answer, then it seems that will make it a correct answer, and I'll select it as the accepted answer - barring a answer that shows it **can** be done. Fair enough? – Seamus May 02 '20 at 16:59
  • 1
    That `${parameter/pattern/string}` expansion seems to also work in ksh93, mksh, yash, zsh (non-exhaustive list). – fra-san May 07 '20 at 09:11
  • @fra-san ah cool, good to know, thanks. I always use bash anyway and since I dug this out of bash documentation, left a warning so that the reader doesn't exist this to be portable. I'll edit. – Pedro May 07 '20 at 14:44