-   Linux - General (
-   -   changing variables to numeric values (

budgie26 01-28-2013 11:24 PM

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?

linosaurusroot 01-29-2013 04:37 AM

budgie26 01-29-2013 05:18 PM

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.

colucix 01-29-2013 05:57 PM

Which version of csh are you running? Is it tcsh?

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.

#!/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:

#!/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.

budgie26 01-29-2013 06:14 PM

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?

suicidaleggroll 01-29-2013 06:51 PM

expr only works on integers, try "bc -l" for floating point math


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

colucix 01-30-2013 02:11 AM

As suicidaleggroll said. Or since you're using awk, let it do the math for you:

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:

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:

set y_value = `awk -v x=$x_value -v s=$size 'BEGIN{print x*s}"`

All times are GMT -5. The time now is 03:04 AM.