Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Best way to truncate/split a var in BASH shell script?

Best way to truncate/split a var in BASH shell script?
Thread Tools
Mac Enthusiast
Join Date: Apr 2001
Status: Offline
Reply With Quote
Feb 11, 2005, 04:21 PM
 
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
Reply With Quote
Feb 11, 2005, 06:24 PM
 
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"

     
Groovy  (op)
Mac Enthusiast
Join Date: Apr 2001
Status: Offline
Reply With Quote
Feb 11, 2005, 09:54 PM
 
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
Reply With Quote
Feb 12, 2005, 01:21 AM
 
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"

     
P
Moderator
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status: Offline
Reply With Quote
Feb 13, 2005, 01:47 PM
 
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
Reply With Quote
Feb 16, 2005, 11:12 PM
 
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
Reply With Quote
Feb 17, 2005, 10:51 AM
 
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"

     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:12 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2