Sponsored Content
Full Discussion: Converting Char to Numeric
Top Forums Shell Programming and Scripting Converting Char to Numeric Post 302358471 by varontron on Friday 2nd of October 2009 04:28:33 PM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
expr(1B)					     SunOS/BSD Compatibility Package Commands						  expr(1B)

NAME
expr - evaluate arguments as a logical, arithmetic, or string expression SYNOPSIS
/usr/ucb/expr argument... DESCRIPTION
The expr utility evaluates expressions as specified by its arguments. After evaluation, the result is written on the standard output. Each token of the expression is a separate argument, so terms of the expression must be separated by blanks. Characters special to the shell must be escaped. Note: 0 is returned to indicate a zero value, rather than the null string. Strings containing blanks or other special characters should be quoted. Integer-valued arguments may be preceded by a unary minus sign. Internally, integers are treated as 32-bit, two's-complement numbers. The operators and keywords are listed below. Characters that need to be escaped are preceded by `'. The list is in order of increasing precedence, with equal precedence operators grouped within {} symbols. expr | expr Returns the evaluation of the first expr if it is neither NULL nor 0; otherwise, returns the evaluation of the second expr if it is not NULL; otherwise, 0. expr & expr Returns the first expr if neither expr is NULL or 0, otherwise returns 0. expr { =, , , <, <=, != } expr Returns the result of an integer comparison if both arguments are integers, otherwise returns the result of a lexical comparison. expr { +, - } expr Addition or subtraction of integer-valued arguments. expr { , /, % } expr Multiplication, division, or remainder of the integer-valued arguments. string : regular-expression match string regular-expression The two forms of the matching operator above are synonymous. The matching operators : and match compare the first argument with the second argument which must be a regular expression. Regular expression syntax is the same as that of regexp(5), except that all pat- terns are "anchored" (treated as if they begin with ^) and therefore ^ is not a special character, in that context. Normally, the matching operator returns the number of characters matched (0 on failure). Alternatively, the ... pattern symbols can be used to return a portion of the first argument. substr string integer-1 integer-2 Extracts the substring of string starting at position integer-1 and of length integer-2 characters. If integer-1 has a value greater than the length of string, expr returns a null string. If you try to extract more characters than there are in string, expr returns all the remaining characters from string. Beware of using negative values for either integer-1 or integer-2 as expr tends to run forever in these cases. index string character-list Reports the first position in string at which any one of the characters in character-list matches a character in string. length string Returns the length (that is, the number of characters) of string. ( expr ) Parentheses may be used for grouping. EXAMPLES
Example 1 Adding an integer to a shell variable Add 1 to the shell variable a. a='expr $a + 1' Example 2 Returning a path name segment Return the last segment of a path name (that is, the filename part). Watch out for / alone as an argument: expr will take it as the divi- sion operator (see BUGS below). # 'For $a equal to either "/usr/abc/file" or just "file"' expr $a : '.*/ $a Example 3 Using // characters to simplify the expression The addition of the // characters eliminates any ambiguity about the division operator and simplifies the whole expression. # A better representation of example 2. expr //$a : '.*/ Example 4 Returning the value of a variable Returns the number of characters in $VAR. expr $VAR : '.*' EXIT STATUS
expr returns the following exit codes: 0 If the expression is neither NULL nor 0. 1 If the expression is NULL or 0. 2 For invalid expressions. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWscpu | +-----------------------------+-----------------------------+ SEE ALSO
sh(1), test(1), attributes(5), regexp(5) DIAGNOSTICS
syntax error for operator/operand errors non-numeric argument if arithmetic is attempted on such a string division by zero if an attempt to divide by zero is made BUGS
After argument processing by the shell, expr cannot tell the difference between an operator and an operand except by the value. If $a is an =, the command: expr $a = '=' looks like: expr = = = as the arguments are passed to expr (and they will all be taken as the = operator). The following works: expr X$a = X= Note: the match, substr, length, and index operators cannot themselves be used as ordinary strings. That is, the expression: example% expr index expurgatorious length syntax error example% generates the `syntax error' message as shown instead of the value 1 as you might expect. SunOS 5.11 6 Jun 2000 expr(1B)
All times are GMT -4. The time now is 05:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy