Help with conky and sed|grep|head|tail|cat|cut

I’m trying to get the CPU frequency in MHz to include it in Conky.

The command I’m using is: cat /proc/cpuinfo | grep "^[c]pu MHz

cpu MHz		: 3593.772
cpu MHz		: 3602.633
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 3568.188
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 3596.717
cpu MHz		: 550.000
cpu MHz		: 3592.460
cpu MHz		: 550.000

I’m trying to get one line per time to include in my conky.conf… Something like this:
Core0: 3593.772 MHz
Core1: 3602.633 MHz

I’m currently stuck with this cat /proc/cpuinfo | grep "^[c]pu MHz" | head -1 | cut -c12-19
This shows the first line, how can I get the second line, third and etc?

Is there a way to accomplish this?

Thanks.t

hmm, I think I found a way…

thread 16: cat /proc/cpuinfo | grep -m 16 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 15: cat /proc/cpuinfo | grep -m 15 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 14: cat /proc/cpuinfo | grep -m 14 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 13: cat /proc/cpuinfo | grep -m 13 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 12: cat /proc/cpuinfo | grep -m 12 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 11: cat /proc/cpuinfo | grep -m 11 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 10: cat /proc/cpuinfo | grep -m 10 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 09: cat /proc/cpuinfo | grep -m 9 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 08: cat /proc/cpuinfo | grep -m 8 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 07: cat /proc/cpuinfo | grep -m 7 “^[c]pu MHz” | tail -1 | cut -c12-19
thread 06: cat /proc/cpuinfo | grep -m 6 “^[c]pu MHz” | tail -1 | cut -c12-19
and goes on…

What does this show?

cat /proc/cpuinfo | grep "^[c]pu MHz" | head -18

Show all of them…

$ cat /proc/cpuinfo | grep "^[c]pu MHz" | head -18
cpu MHz		: 3590.813
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 3591.224
cpu MHz		: 550.000
cpu MHz		: 3589.665
cpu MHz		: 3599.029
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 550.000
cpu MHz		: 3590.804
cpu MHz		: 550.000
cpu MHz		: 550.000

  1. c ↩︎

Conky has this built in:

freq
[cpu_number]

Returns CPU #n's frequency in MHz. CPUs are counted from 1. If omitted, the parameter defaults to 1.
${freq}MHz
freq_g
[cpu_number]

Returns CPU #n's frequency in GHz. CPUs are counted from 1. If omitted, the parameter defaults to 1.
${freq_g}Ghz

Check out http://www.ifxgroup.net/conky.htm for more details on Conky commands/variables.

2 Likes

haha, really thanks bro :slight_smile:

1 Like

In general:

cat "file" | grep 'pattern'

is an anti-pattern called Useless Use of Cat. You should always avoid it. The grep utility can take a file name, so you should use this instead:

grep 'pattern' "file"

Also, notice that the 'pattern' argument is in single quotes, while the "file" argument is in double quotes. This is because we never want shell globbing in the pattern argument, but often we do want it in the filename argument.

Rarely, a utility like grep does not accept a filename as an argument, and it only works with stdin. Not even in this case should you use cat to read the file, because the shell has input/output redirection for that:

command < "file"

Probably the most useless use of cat is for reading a file and saving it into a variable, like this:

variable=$(cat "file")

Here, shell input redirection also works better:

variable=$(<"file")

In general, it is best practice to completely forget that the cat utility prints the file to stdout. That is not its purpose and it shouldn’t be used that way (though, in an interactive shell session, there is no harm, except to one’s self-esteem).

There is always a better way to read a file than using cat. Instead, cat should only be used to concatenate two or more files into one. When you have only one file, it is a completely useless tool.

5 Likes

cat me harder, daddy!!!11

honka_animated-128px-14

3 Likes

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