Sponsored Content
Top Forums Shell Programming and Scripting Converting String To Integer/Float (weird case) Post 302989283 by Don Cragun on Tuesday 10th of January 2017 08:35:46 PM
Old 01-10-2017
It isn't really clear to me what you're trying to do in cases where there is no <hyphen> character in your output from awk. Is a return value like 000000234 to remain unchanged or do you want to remove leading zeros?

If you always want to remove leading zeros (and never have a string that is all zeros and don't want to change your awk script, you don't need to use bc to perform your arithmetic; it can all be done with variable expansions:
Code:
$ x=000000234
$ echo ${x##*[^1-9-]}
234
$ x=0-123456
$ echo ${x##*[^1-9-]}
-123456
$ 

These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Integer/Float Script Problem

Hi, I have a script which takes a value from a file and performs calculations on it. Trouble is that this value is a float not an integer and it errors at the decimal point! eg. 94.62 I would like to be able to detect the length of the float (in this above case, 5 characters), and simply do a... (2 Replies)
Discussion started by: danhodges99
2 Replies

2. Shell Programming and Scripting

command for converting string to integer

Hi ... I am trying to calculate the time needed for a command to execute.. but the resulting value is getting as string.. so i am not able to use "expr " command.. please help me to convert the value to integer so that i can proceed with my script.. Regards esham (1 Reply)
Discussion started by: esham
1 Replies

3. Shell Programming and Scripting

Converting the case of the string?

Hi i have a string value in a variable STR eg. STR="ABSDSCSFS" How can be convert it to LOWERCASE in the script? Thanks (5 Replies)
Discussion started by: skyineyes
5 Replies

4. UNIX for Dummies Questions & Answers

Converting a String variable into Integer

Hi, I am passing a variable to a unix function. However when I try to assign the value to another variable like typeset -i I_CACHE_VAL=$2 Is this because of String to Integer conversion? I get an error. Please help me with thsi. Thanks (2 Replies)
Discussion started by: neeto
2 Replies

5. Shell Programming and Scripting

Converting integer to String

Hi everyone, I would like to know how to convert an integer to a string. for instance if i=1 i would like to creat a variable called constant1. i want to do this in a for loop so for each value of i, i create a new variable such as constant2, constant3,... and so on. for i in 1 2 3 do ... (1 Reply)
Discussion started by: ROOZ
1 Replies

6. UNIX for Advanced & Expert Users

byte swapping 32-bit float and weird od results

I'm attempting to read a file that is composed of complex 32-bit floating point values on Solaris 10 that came from a 64-bit Red Hat computer. When I first tried reading the file, it looked like there was a byte-swapping problem and after running the od command on the file Solaris and Red Hat... (2 Replies)
Discussion started by: GoDonkeys
2 Replies

7. Shell Programming and Scripting

converting to lower case or upper case

here is a code column_name="vivek" column_name2="ViVeK" column_name=$(echo $column_name | awk '{print tolower($0)}') column_name2=$(echo $column_name2 | awk '{print tolower($0)}') echo "column name 1 lower: $column_name" echo "column name... (6 Replies)
Discussion started by: vivek d r
6 Replies

8. Shell Programming and Scripting

Converting Month into Integer

okay so i run an openssl command to get the date of an expired script. Doing so gives me this: enddate=Jun 26 23:59:59 2012 GMT Then i cut everything out and just leave the month which is "Jun" Now the next part of my script is to tell the user if the the certificate is expired or not... (6 Replies)
Discussion started by: shade917
6 Replies

9. Shell Programming and Scripting

Converting date +%j into integer

Dear community, i got a problem to get "date +%j" as the right value. Today is the 10th day of the year. #./script.sh 2 #!/bin/bash/ Var1=$(date +%j) Var2=$1 let result=$Var1+$Var2 echo $Var1 plus $Var2 equals $result The output of the script is: 010 plus 2 equals 10... (9 Replies)
Discussion started by: OskarHF
9 Replies

10. Shell Programming and Scripting

Converting string to integer

I have a function that is supposed to check for user processes and wait for 0 count before exiting the function. I am sure I have more than one issue in my code, but the stumbling block right now is that I am trying to convert the value of my variable from a string to integer. process_count... (10 Replies)
Discussion started by: MIA651
10 Replies
typeset(1)							   User Commands							typeset(1)

NAME
typeset, whence - shell built-in functions to set/get attributes and values for shell variables and functions SYNOPSIS
typeset [ +- HLRZfilrtux [n]] [ name [ = value]]... whence [-pv] name... DESCRIPTION
typeset sets attributes and values for shell variables and functions. When typeset is invoked inside a function, a new instance of the variables name is created. The variables value and type are restored when the function completes. The following list of attributes may be specified: -H This flag provides UNIX to host-name file mapping on non-UNIX machines. -L Left justify and remove leading blanks from value. If n is non-zero it defines the width of the field; otherwise, it is determined by the width of the value of first assignment. When the variable is assigned to, it is filled on the right with blanks or trun- cated, if necessary, to fit into the field. Leading zeros are removed if the -Z flag is also set. The -R flag is turned off. -R Right justify and fill with leading blanks. If n is non-zero it defines the width of the field, otherwise it is determined by the width of the value of first assignment. The field is left filled with blanks or truncated from the end if the variable is reas- signed. The -L flag is turned off. -Z Right justify and fill with leading zeros if the first non-blank character is a digit and the -L flag has not been set. If n is non-zero it defines the width of the field; otherwise, it is determined by the width of the value of first assignment. -f The names refer to function names rather than variable names. No assignments can be made and the only other valid flags are -t, -u and -x. The flag -t turns on execution tracing for this function. The flag -u causes this function to be marked undefined. The FPATH variable will be searched to find the function definition when the function is referenced. The flag -x allows the func- tion definition to remain in effect across shell procedures invoked by name. -i Parameter is an integer. This makes arithmetic faster. If n is non-zero it defines the output arithmetic base; otherwise, the first assignment determines the output base. -l All upper-case characters are converted to lower-case. The upper-case flag, -u is turned off. -r The given names are marked readonly and these names cannot be changed by subsequent assignment. -t Tags the variables. Tags are user definable and have no special meaning to the shell. -u All lower-case characters are converted to upper-case characters. The lower-case flag, -l is turned off. -x The given names are marked for automatic export to the environment of subsequently-executed commands. The -i attribute can not be specified along with -R, -L, -Z, or -f. Using + rather than - causes these flags to be turned off. If no name arguments are given but flags are specified, a list of names (and optionally the values) of the variables which have these flags set is printed. (Using + rather than - keeps the values from being printed.) If no names and flags are given, the names and attributes of all variables are printed. For each name, whence indicates how it would be interpreted if used as a command name. The -v flag produces a more verbose report. The -p flag does a path search for name even if name is an alias, a function, or a reserved word. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
ksh(1), set(1), sh(1), attributes(5) SunOS 5.10 1 Feb 1995 typeset(1)
All times are GMT -4. The time now is 02:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy