LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-14-2007, 11:38 PM   #1
falcon56215
Member
 
Registered: Jan 2002
Location: Tennessee
Distribution: Xubuntu Natty on Lenovo R61i Thinkpad
Posts: 108

Rep: Reputation: 16
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?
 
Old 02-15-2007, 12:15 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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'.

Last edited by bigrigdriver; 02-15-2007 at 12:16 AM.
 
Old 02-15-2007, 09:26 AM   #3
falcon56215
Member
 
Registered: Jan 2002
Location: Tennessee
Distribution: Xubuntu Natty on Lenovo R61i Thinkpad
Posts: 108

Original Poster
Rep: Reputation: 16
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.
 
Old 02-15-2007, 01:28 PM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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.

Last edited by bigrigdriver; 02-15-2007 at 01:38 PM.
 
Old 02-15-2007, 01:35 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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/
 
Old 02-16-2007, 06:36 PM   #6
falcon56215
Member
 
Registered: Jan 2002
Location: Tennessee
Distribution: Xubuntu Natty on Lenovo R61i Thinkpad
Posts: 108

Original Poster
Rep: Reputation: 16
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.
 
Old 02-16-2007, 09:14 PM   #7
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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.

Last edited by bigrigdriver; 02-16-2007 at 09:15 PM.
 
Old 02-16-2007, 10:39 PM   #8
falcon56215
Member
 
Registered: Jan 2002
Location: Tennessee
Distribution: Xubuntu Natty on Lenovo R61i Thinkpad
Posts: 108

Original Poster
Rep: Reputation: 16
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?
 
Old 02-16-2007, 11:06 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 02-16-2007, 11:20 PM   #10
falcon56215
Member
 
Registered: Jan 2002
Location: Tennessee
Distribution: Xubuntu Natty on Lenovo R61i Thinkpad
Posts: 108

Original Poster
Rep: Reputation: 16
Genius..... now what was that about trying to reinvent the wheel?

Thanks, that did it. Every day I learn just a tad more.
 
Old 02-17-2007, 01:58 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert doc to pdf in command line yumener Linux - Software 4 04-28-2006 08:23 AM
Convert Excel to CVS from command line? technick Linux - Software 0 12-27-2005 03:55 PM
command line utility to convert between formats like .doc, .sxw, .rtf and others? bigtpumped Linux - Software 1 09-12-2005 09:54 PM
how to convert charcter to integer husniteja Programming 6 08-30-2004 09:29 AM
mp3 batch convert, remote from command line zerodot Linux - Networking 1 02-12-2003 05:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration