LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 01-28-2013, 11:24 PM   #1
budgie26
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Rep: Reputation: Disabled
changing variables to numeric values


I know that this probably has a really simple solution, but I've spent all afternoon on it and just can't work it out.

I have a set of variables which are numbers in a text file, and I want to do a calculation with these variables, but I get 'non-numeric argument' as the answer.

The code is (written in csh):

set file = results.txt
set x-value = `awk 'NR==3 {print $2}' $file`
set size = `awk 'NR==4 {print $2}' $file`
set y-value = `expr $x-value \* $size`

If I echo $x-value and $size, I get the right numeric results, so I know that part of the code is working. It's the $y-value that's the problem. I want to multiply $x-value with $size.

Any ideas?
 
Old 01-29-2013, 04:37 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 870
Blog Entries: 2

Rep: Reputation: 219Reputation: 219Reputation: 219
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
 
Old 01-29-2013, 05:18 PM   #3
budgie26
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Yes I know some people think that csh isn't ideal, but I'm updating an existing script and don't have the time to re-write the script at the moment. So any advice on how to resolve this in csh would be appreciated.
 
Old 01-29-2013, 05:57 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Which version of csh are you running? Is it tcsh?
Code:
csh --version
It's hard to tell what the problem is without taking a look at the input. Please, can you post the first 4 lines of results.txt (using CODE tags)?

As a general advice, to debug your script you may add the -x option to the shell invocation. It causes the shell print out the command after every substitution made and before executing them. In this way you can see if and when weird or unexpected values appear at some point. Verify the actual path of your csh command and put a proper sha-bang at the beginning of your script, e.g.
Code:
#!/bin/csh -x
Second, I don't know if it is a typo, but the minus sign is not allowed in variable names. It should be an underscore, instead:
Code:
#!/bin/csh -x
set file = results.txt
set x_value = `awk 'NR==3 {print $2}' $file`
set size = `awk 'NR==4 {print $2}' $file`
set y_value = `expr $x_value \* $size`
echo $y_value
This code works for me, giving proper input and using tcsh 6.17.00.

Last edited by colucix; 01-29-2013 at 06:00 PM.
 
Old 01-29-2013, 06:14 PM   #5
budgie26
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
The csh version is: tcsh 6.14.00.

The 1st 4 lines of results.txt are:

title results
date 29/01/2013
x_value 148.9998611111
size 0.000277777

It was typo, the script is using underscore.

I added -x and looks like it doesn't appear to be picking up the $size value:

set y_value = `expr $x-value \* $size`
148.998611111 *

Just thinking, is it because the size variable starts with 0?
 
Old 01-29-2013, 06:51 PM   #6
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,212

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
expr only works on integers, try "bc -l" for floating point math

Code:
$ expr 5 \* 6
30
$ expr 5.0 \* 6
expr: non-numeric argument
$ echo "5.0 * 6" | bc -l
30.0

Last edited by suicidaleggroll; 01-29-2013 at 06:54 PM.
 
Old 01-30-2013, 02:11 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
As suicidaleggroll said. Or since you're using awk, let it do the math for you:
Code:
#!/bin/csh
set file = results.txt
set y_value = `awk 'NR==3{x_value=$2} NR==4{size=$2} END{print x_value*size}' $file`
However if you need the intermediate variables in the follow-up of the script:
Code:
#!/bin/csh
set file = results.txt
set x_value = `awk 'NR==3 {print $2}' $file`
set size = `awk 'NR==4 {print $2}' $file`
set y_value = `awk "BEGIN{print $x_value*$size}"`
Please notice the double quotes in the last awk command in order to let the shell substitute the values of size and x_value. A formally more correct version is:
Code:
set y_value = `awk -v x=$x_value -v s=$size 'BEGIN{print x*s}"`
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need to parse numeric values and file paths from a security scan output. BobbyinMDC Programming 9 08-27-2012 11:06 AM
bash: how to compare REAL numeric values? muebi Linux - General 3 02-28-2012 09:00 AM
BASH scripting: check for numeric values linuxLuser Programming 11 11-14-2011 11:11 AM
Changing my numeric user id openSauce Linux - General 6 02-11-2009 07:07 PM
Searching for numeric values in PHP TheBeli Programming 5 04-17-2008 04:55 AM


All times are GMT -5. The time now is 07: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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration