 |
 |
Best way to truncate/split a var in BASH shell script?
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2001
Status:
Offline
|
|
what is the best way take the original returned float and then do the follow
example
$ORIGINAL = 42.27364658689796977
I want
Var1 = 42
Var2 = .27364658689796977
Thoughts?
Thx
P.S. yeah yeah Bash newbie 
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status:
Offline
|
|
Originally posted by Groovy:
what is the best way take the original returned float and then do the follow
example
$ORIGINAL = 42.27364658689796977
I want
Var1 = 42
Var2 = .27364658689796977
I take it that the integer part of the return is not always going to be 2 digits, right? That'd be too easy. This isn't very robust, and I know there's probably ways built into awk to do this automatically, but here:
Code:
var1=`echo $ORIGINAL | awk -F. '{print $1}'`
var2=`echo $ORIGINAL | awk -F. '{print " ."$2}'`
Like I said, there's easier ways to do this if you have better awk skills (I don't).
The "-F." tells awk to split the input into columns based on the delimiter following the "-F" option, in this case it's the period. Awk now sees the text as having two columns. The "print $1" prints the first column and then the second one does similar, only printing the second column with a leading ".". The backticks ( ` surrounding the entire command tell the shell to pump the result of that command into the variable (var1 and var2).
This stuff's hard to write about without it getting ugly. Let me know if this is unclear. 
|
"In Nomine Patris, Et Fili, Et Spiritus Sancti"
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2001
Status:
Offline
|
|
Originally posted by [APi]TheMan:
I take it that the integer part of the return is not always going to be 2 digits, right?
mostly 2 but yes there is a chance of 3 digits but that would be rare
Originally posted by [APi]TheMan:
That'd be too easy. This isn't very robust, and I know there's probably ways built into awk to do this automatically, but here:
Code:
var1=`echo $ORIGINAL | awk -F. '{print $1}'`
var2=`echo $ORIGINAL | awk -F. '{print " ."$2}'`
Like I said, there's easier ways to do this if you have better awk skills (I don't). 
The "-F." tells awk to split the input into columns based on the delimiter following the "-F" option, in this case it's the period. Awk now sees the text as having two columns. The "print $1" prints the first column and then the second one does similar, only printing the second column with a leading ".". The backticks (` surrounding the entire command tell the shell to pump the result of that command into the variable (var1 and var2).
This stuff's hard to write about without it getting ugly. Let me know if this is unclear.
i'll give it go
thanks
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status:
Offline
|
|
Originally posted by Groovy:
mostly 2 but yes there is a chance of 3 digits but that would be rare
If it's going to be only two digits for the integer part, it'd probably be much easier to just use something like:
Code:
var1=`echo ${ORIGINAL:0:2}`
var2=`echo ${ORIGINAL:2}`
This syntax is a function of the shell on variable expansion. The first one starts at character 0 and goes for 2 more, then the second one prints from character 2 to until the end of the variable. Very cool.

|
"In Nomine Patris, Et Fili, Et Spiritus Sancti"
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status:
Offline
|
|
I'd use cut, it's a bit easier to understand. In your case you'd write
cut -f 1 -d . <infile >outfile
for the integer part and
cut -f 2 -d . <infile >outfile
for the fraction.
See "man cut" for details.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
Yeah I'm amazed awk was suggested before cut. That's cake with cut.
Note that you can also pipe through cut. It's not as powerful as awk, but it's way easier to use for simple things like this.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status:
Offline
|
|
Originally posted by wataru:
Yeah I'm amazed awk was suggested before cut. That's cake with cut.
Note that you can also pipe through cut. It's not as powerful as awk, but it's way easier to use for simple things like this.
I never learned how to use cut. 
|
"In Nomine Patris, Et Fili, Et Spiritus Sancti"
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|