Visit Our UNIX and Linux User Community


Converting Char to Numeric


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting Char to Numeric
# 1  
Old 10-02-2009
Converting Char to Numeric

HI,
Here I have the following output and want to do some mathematical calculation on C2 & C3 column.
Code:
  
         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.

Last edited by jim mcnamara; 10-02-2009 at 08:11 AM.. Reason: code tags
# 2  
Old 10-02-2009
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

Code:
$ 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.
# 3  
Old 10-02-2009
Quote:
Originally Posted by 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

Code:
$ 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.
# 4  
Old 10-02-2009
Quote:
Originally Posted by dear_abhi2007
HI,
Here I have the following output and want to do some mathematical calculation on C2 & C3 column.
Code:
  
         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):

Code:
echo $(( $3 - $2 ))

Quote:
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.

Use this function to convert the time to seconds. The result is stored in $_SECS.

Code:
t2secs()
{
  IFS=: read hour minutes seconds <<.
$1
.
_SECS=$(( $hours * 3600 + $minutes * 60 + $seconds ))
}


Last edited by cfajohnson; 10-02-2009 at 06:46 PM..
# 5  
Old 10-02-2009
The shell works great for integers. That wasn't specified by dear_abhi2007
Code:
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

# 6  
Old 10-02-2009
Quote:
Originally Posted by varontron
The shell works great for integers. That wasn't specified by dear_abhi2007

Which is why I said, "assuming integers", and then showed how to convert it to integers.

Previous Thread | Next Thread
Test Your Knowledge in Computers #805
Difficulty: Medium
The goal of a Digital Signal Processor (DSP) is usually to measure, filter or compress discrete digital signals.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to add a numeric & special char to end of the first line

Need to add a numeric & special char to end of the first line Existing file: 12-11-16|11 2016 Jan 12:34:55|03:55| 13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55| 14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55| 15-10-16|18 2016 Jan... (11 Replies)
Discussion started by: Joselouis
11 Replies

2. Shell Programming and Scripting

Parsing of Char and Numeric in a file

Hi All, i'm working on some report and currently have this plain text file generated. server_name1|sdfd1deal | 1048572| 1040952| 99| 207| 1| 1 server_name1|dba1dbs | 83886048| 40730796| 48| 5762| 22764| 8... (4 Replies)
Discussion started by: fedora132010
4 Replies

3. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

4. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

6. Programming

After converting the hexstr to Hex and storing the Hex in a char*

Hi All, My main intension of is to convert the Hexstring stored in a char* into hex and then prefixing it with "0x" and suffix it with ',' This has to be done for all the hexstring char* is NULL. Store the result prefixed with "0x" and suffixed with ',' in another char* and pass it to... (1 Reply)
Discussion started by: rvan
1 Replies

7. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

8. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

9. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

10. Shell Programming and Scripting

How to check a column contain numeric or char data type ??

I have a file called clientname_filename.csv whose contents are like col1|col2|col3|col4| 510|abc|xxx|450| 510|abc11|yyy|350 510|pqr99|zzz| 670 512|222|439|110 Here i have check the contents of column for data type. i have a constraints that col1 always contain Numeric value column 2... (12 Replies)
Discussion started by: jambesh
12 Replies

Featured Tech Videos