HI,
Here I have the following output and want to do some mathematical calculation on C2 & C3 column.
c1 c2 c3 c4 c5
l1 1-oct 12:30:01 12:35 abc xyz
l2 1-oct 14:20:01 14:35 def pqr
l3 2-oct 23:50:01 00:05 ghi mno
O/p I need as two column inserted in-between the above o/p. i have 50 records like this.
C1 C2 C3 C3-C2 C3(l2) - C3(l1) C4 C5
But when I use SET command it is treating the values as character & I am not able to do any operation.
echo `expr $3 - $2`
expr: non-numeric argument
So do we have any other command which can help me in this process or any cmd by which I can convert it to numeric value.
you can likely convert your time values to seconds using the 'date' command, or do the math yourself
if the results of your expressions are all integers, your 'expr' commands will be ok.
otherwise, you can do all kinds of math using the 'dc' command
$ dc -e "2 k 3 2 / p"
1.50
the 'dc' command takes a somewhat arcane arg list for setting precision, printing results, etc. It also uses postfix ops which pop entries off the stack. check out the man page, or the wikipedia page.
varontron:
you can likely convert your time values to seconds using the 'date' command, or do the math yourself
if the results of your expressions are all integers, your 'expr' commands will be ok.
otherwise, you can do all kinds of math using the 'dc' command
$ dc -e "2 k 3 2 / p"
1.50
the 'dc' command takes a somewhat arcane arg list for setting precision, printing results, etc. It also uses postfix ops which pop entries off the stack. check out the man page, or the wikipedia page.
Hi,
i tried with the above cmd but didn't succeeded.
dc -e "2 k 3 2 / p" temp1
Cannot stat 2 k 3 2 / p: No such file or directory
And can you plz help how can i convert my date to Time into Sec.
dear_abhi2007:
HI,
Here I have the following output and want to do some mathematical calculation on C2 & C3 column.
c1 c2 c3 c4 c5
l1 1-oct 12:30:01 12:35 abc xyz
l2 1-oct 14:20:01 14:35 def pqr
l3 2-oct 23:50:01 00:05 ghi mno
O/p I need as two column inserted in-between the above o/p. i have 50 records like this.
C1 C2 C3 C3-C2 C3(l2) - C3(l1) C4 C5
But when I use SET command it is treating the values as character & I am not able to do any operation.
echo `expr $3 - $2`
There is no need to use an external command to do arithmetic; use the shell (assuming that you have digits):
echo $(( $3 - $2 ))
Use this function to convert the time to seconds. The result is stored in $_SECS.
t2secs()
{
IFS=: read hour minutes seconds <<.
$1
.
_SECS=$(( $hours * 3600 + $minutes * 60 + $seconds ))
}
The shell works great for integers. That wasn't specified by dear_abhi2007
sh-3.2$ A=10
sh-3.2$ B=6
sh-3.2$ echo $(( $A - $B ))
4
sh-3.2$ A=10.7
sh-3.2$ echo $(( $A - $B ))
sh: 10.7 - 6 : syntax error: invalid arithmetic operator (error token is ".7 - 6 ")
sh-3.2$ dc -e "1 k $A $B - p"
4.7
Which is why I said, "assuming integers", and then showed how to convert it to integers.