LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-19-2009, 05:28 PM   #1
zebrafx
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
command out to a variable


Hopefully this wasn't asked like a million times, tried searching but haven't found the right solution. I generally want to output a number from a linux command into a variable. Basically it would generate a couple lines of text and then on the last line output:

tcp: 0 bytes in 0.00 real seconds = 3242.00 KB/sec +++

I want to get that 3242.00 value into a variable. It going to be a script that basically will like add up 20 of them and give the average.

Thanks for reading this message.

Edit: I forgot to mention I am using shell script.

Last edited by zebrafx; 02-19-2009 at 05:30 PM. Reason: forgot the language
 
Old 02-19-2009, 05:35 PM   #2
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 shell does not manage floating point numbers, hence you have to use some external commands like bc or awk. Here is a simple awk solution:
Code:
awk '{sum+=$9;count++}END{print sum/count}' file
this requires the input is always formatted as in your example, that is the requested number is the ninth field in each line of the file.
 
Old 02-19-2009, 06:08 PM   #3
zebrafx
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
The shell does not manage floating point numbers, hence you have to use some external commands like bc or awk. Here is a simple awk solution:
Code:
awk '{sum+=$9;count++}END{print sum/count}' file
this requires the input is always formatted as in your example, that is the requested number is the ninth field in each line of the file.
I am not sure how to use awk. I looked up awk and had trouble finding examples to my situation so I am not sure on the syntaxing.

Basically I was using prior:
/usr/local/bin/istcp -t $iprec -p 20000 | grep KB/sec

and it outputs:
tcp: 0 bytes in 0.00 real seconds = 0.00 KB/sec +++

I tried the code below and
Code:
/usr/local/bin/istcp -t $iprec -p 20000 | awk {sum+=$9;count++}END{print sum/count}
I get an error below.
./tcptest: line 12: unexpected EOF while looking for matching `''
./tcptest: line 17: syntax error: unexpected end of file
 
Old 02-20-2009, 12:27 AM   #4
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
Well, the error is due to the fact you missed the single quotes around the awk program. Look at my previous post: single quotes are mandatory.

Anyway, I misinterpreted your first post, since you talk about calculating the mean of 20 numbers, so I thought you had a file with 20 lines. My awk command line calculates the mean of the number in the ninth field of each line.

From your last example, I think you want help just for the first part of your issue, that is to store the number in a shell variable. To do this use command substitution:
Code:
var=$(command)
which assigns the output of the command to the variable. To extract the number from the line, again you can use awk or eventually the command cut. For example:
Code:
var=$(/usr/local/bin/istcp -t $iprec -p 20000 | grep KB/sec | awk '{print $9}')
 
Old 02-20-2009, 09:12 AM   #5
zebrafx
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Well, the error is due to the fact you missed the single quotes around the awk program. Look at my previous post: single quotes are mandatory.

Anyway, I misinterpreted your first post, since you talk about calculating the mean of 20 numbers, so I thought you had a file with 20 lines. My awk command line calculates the mean of the number in the ninth field of each line.

From your last example, I think you want help just for the first part of your issue, that is to store the number in a shell variable. To do this use command substitution:
Code:
var=$(command)
which assigns the output of the command to the variable. To extract the number from the line, again you can use awk or eventually the command cut. For example:
Code:
var=$(/usr/local/bin/istcp -t $iprec -p 20000 | grep KB/sec | awk '{print $9}')
Ah perfect. Ok, I was messing around a bit with awk and I had something close to what you posted. It didn't work though lol. Thanks for the help, I now understand how to use it.
 
  


Reply



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
~/.bashrc variable for a command? Maybe? computer_freak_8 Linux - Newbie 17 02-02-2009 07:46 PM
variable in a tail command jadeddog Programming 4 06-29-2008 09:35 PM
parsing a variable from here command tostay2003 Programming 5 12-26-2007 08:17 PM
How do I turn my variable into a command? spiffytech Linux - Software 3 12-29-2005 12:30 PM
Command & Variable karunesh Programming 5 04-08-2005 06:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:45 PM.

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