LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I convert to an Integer with the Command Line? (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-convert-to-an-integer-with-the-command-line-529183/)

falcon56215 02-14-2007 11:38 PM

How do I convert to an Integer with the Command Line?
 
I need to show my CPU speed as an integer. I can get the following output:

[root@localhost falcon56215]# cat /proc/cpuinfo | grep 'cpu MHz' | awk '{print $4}'
1855.182
[root@localhost falcon56215]#

I somehow need to add another pipe to get this to the integer 1855. Any Ideas?

bigrigdriver 02-15-2007 12:15 AM

From the Effective AWK Programming guide, page 136: use the int(x) function, like so:

cat /proc/cpuinfo | grep 'cpu MHz' | awk '{print int($4)}'

Note: the space in 'cpu MHz' is there because that's how it's printed when I run 'cat /proc/cpuinfo'.

falcon56215 02-15-2007 09:26 AM

Thank you, bigrigdriver. That worked great. Is that an online guide, or a hardcopy book? If it is a book, can you give me the ISBN#? I would like to purchase it. Also, any other books you could recommend for basic Linux knowledge would be great. Again, thanks for the help.

bigrigdriver 02-15-2007 01:28 PM

The Effective AWK Programming guide is free, both on-line and download. Just use www.google.com/linux to search for it.

Also look for (all free):
Absolute Bash-scripting Guide
Bash Guide for Beginners
Effective c++
Thinking in c++
Byte of Python
Dive into Python
Thinking in Pyghon
Rute User's Tutorial
Linux Administration Made Easy

That should keep you busy for a couple of days.

When you're done reading those, let me know. I have a bunch of sites bookmarked, but 7 or 8 stand out as on=line libraries and repositories of tutorials, etc that you can explore.

colucix 02-15-2007 01:35 PM

The "GAWK: Effective AWK Programming" is one of the best books I have ever read. No kidding. Look for the official release at http://www.gnu.org/software/gawk/manual/

falcon56215 02-16-2007 06:36 PM

Another Question about the Command Line
 
Ok, Thanks. I work 40 hours a week, go to school full time, have 3 kids and a wife....but I always find time to read when it comes to Linux. One more question maybe you can answer off the top of your head...

I have a karamba script that displays the temperature of my CPU. It does so with the following command:

$ cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2}'

The output is in celsius, I would like it in Fahrenheit. I have a program that converts from C to F with the following command:

$ tempconv 58 c

would give the temp in F, how can I pipe the previous command into this one? It all has to be on one line. Of course if you know another way using the C to F formula and can get it all in one line, I'm all ears.

Thanks for all the help.

bigrigdriver 02-16-2007 09:14 PM

Try this:

cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2}' | tempconv

Since your tempconv app requires the temp to convert as an input, it will get that input from the awk command.

falcon56215 02-16-2007 10:39 PM

Well, that was a good thought. I tried that but tempconv needs to see a "c" after the temperature to know it is converting from C to F. So I came up with the following command, and look at the outputs:

[falcon56215@localhost ~]$ cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2" c"}'
52 c
[falcon56215@localhost ~]$ cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2" c"}' | tempconv
Temperature Converter
To Convert from Fahrenheit to Celsius press f
To Convert from Celsius to Fahrenheit press c
tempconv - converts a value to/from degrees Farenheit from/to
degrees Celcius.
usage:
tempconv [<value> <f|c>] | [h|help|-h|--help]

with no options, it interactively prompts for values.
or you can give it the necessary info on the command line very simply
and automatically get the right answer back with no other output, for
use in scripts. Examples:
You have a temperature reading from your motherboard cpu sensor in
degrees C. You want to know what that is in degrees F...
you type: tempconv 24 c
tempconv returns: 75
tempconv --help or -h or h or help shows this message.

[falcon56215@localhost ~]$ tempconv 52 c
125
[falcon56215@localhost ~]$ cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2" c"}'
52 c
[falcon56215@localhost ~]$ cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2" c"}' | tempconv
Temperature Converter
To Convert from Fahrenheit to Celsius press f
To Convert from Celsius to Fahrenheit press c
tempconv - converts a value to/from degrees Farenheit from/to
degrees Celcius.
usage:
tempconv [<value> <f|c>] | [h|help|-h|--help]

with no options, it interactively prompts for values.
or you can give it the necessary info on the command line very simply
and automatically get the right answer back with no other output, for
use in scripts. Examples:
You have a temperature reading from your motherboard cpu sensor in
degrees C. You want to know what that is in degrees F...
you type: tempconv 24 c
tempconv returns: 75
tempconv --help or -h or h or help shows this message.

[falcon56215@localhost ~]$

...almost like the pipe is not happening. Any thoughts?

Tinkster 02-16-2007 11:06 PM

Not sure - I don't know the tool and hence can't test it.
But why don't you do this in awk?
Tf = (9/5)*Tc+32


Code:

cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print (9/5)*$2 + 32}'

Cheers,
Tink

falcon56215 02-16-2007 11:20 PM

Genius..... now what was that about trying to reinvent the wheel?

Thanks, that did it. Every day I learn just a tad more.

Tinkster 02-17-2007 01:58 PM

Glad I could be of assistance in that. I strongly believe in
"education for the masses" :} and that "knowledge is power".


Cheers,
Tink


All times are GMT -5. The time now is 10:34 AM.