Use of the tr command

So I want to show the temp of my gpu in polybar and add °C with the following command:

cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input | awk '{sub("$", "°C", $1)}; 1'                                                   0 4ms
35000°C

and remove the last 3 digits to make it clean:

cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input | tr -d "000" | awk '{sub("$", "°C", $1)}; 1'                                     0 3ms
35°C

But when the temp is 40 or 50 etc it also removes the second digit:

cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input | tr -d "000" | awk '{sub("$", "°C", $1)}; 1'                                     0 3ms
4°C

How do I keep only the first 2 digits and remove everything after that cause tr -d “0” removes every 0

tr -d removes characters based on pattern, not strings, so tr -d "0" will remove all zeroes by design. What is the output of cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input? What make/model of GPU?

▶ cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input
33000

It’s a AMD RX6400

Does this command always return 5 digits? If so, then pipe it through cut -b -2.

1 Like
▶ cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input | cut -b -2 | awk '{sub("$", "°C", $1)}; 1'                X 1m23s 
35°C

Yes always 5. Thanks that was the solution I was looking for.

Assuming your output always has trailing ‘000’ as you hinted in your original post and replies (because I’m not otherwise familiar with those files), one minor issue with that solution is that if for example a gpu fan breaks and your GPU goes over 100°C, then the output of the above would be false (always 10<output<100).

for temp in 33000 4000 120000; do
    echo $temp | cut -b -2 | awk '{sub("$", "°C", $1)}; 1'
done

33°C
40°C
12°C

One alternative if you need to handle that case could be (using sed):

for temp in 33000 4000 120000; do
    echo $temp | sed 's/000$/°C/'
done

33°C
4°C
120°C
1 Like

Another way:

temp=$(< /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input); echo ${temp%???}

Edit: sorry, I meant this for @peciwov .

3 Likes

What is the terminology for this form of substitutions (using {})?
I’ve noticed this in eos scripts.
I would like to study a bit on it and learn it, just not sure how to google it :stuck_out_tongue:

1 Like

It is described in the bash manual. In man bash look for:
parameter%word

3 Likes

This link describes your use-case in a nutshell: https://stackoverflow.com/questions/27658675/how-to-remove-last-n-characters-from-a-string-in-bash; like this better than my cut post.

1 Like

By far the best solution. No abuse of cats, no sed, awk, tr, cut, or any such bloat. No stupid plumbing. Only Bash… Beautiful.

1 Like

cat2_b9892408

You could do it shorter :

cat /sys/class/drm/card1/device/hwmon/hwmon*/temp1_input|sed 's/...$/°C/g'
38°C 

…and we’re back to abusing cats, stupid plumbing, and sed bloat. :frowning:

I don’t care much about the “bloat” for displaying a temperature in a bar, using 2 or 3 variables and no pipe or sed don’t make me feel better. :slightly_smiling_face:

You should care about spawing two processes every time you read a temperature.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.