[SOLVED] extracting floating numbers from variable using bash's builtin string chopping
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
extracting floating numbers from variable using bash's builtin string chopping
Hi. I'm wanting to use bash3 to chop up some strings! :-)
Here's the string I want to chop:
Code:
C9-1.4-1.6
, which I'm assigning to a variable that I'm naming $window.
I'm wanting to chop out the 1.4 and the 1.6. The 1.6 is simple enough with something like
Code:
echo "${window##*-}"
which, of course, chops off the beginning of the string until the final -.
What I'm having trouble with is getting the 1.4 out of there. I can do it with a simple
Code:
echo "${window:3:3}"
to pull out the substring, but the thing is, sometimes the window I'm manipulating is something like
Code:
C10-1.4-1.6
, in which case the above will give me
Code:
-1.
which obviously isn't what I'm looking for.
So, to summarize, I have a string that contains two 2-digit floating point numbers, separated off by dashes(-), and I want to extract these and only these strings.
If you use only one hashmark, it stops at the first match.
Edit: After seeing the above post, I think I misunderstood the question. You want to extract only the middle number? I don't think that's generally doable in one step through parameter substitution (except by the 3:3 column matching you already know about). You have to use multiple steps as above, or an external tool like grep, sed, or awk for that.
Last edited by David the H.; 07-16-2009 at 02:05 PM.
Actually, that's rather neat. I should've known that there would be a shell option for things like this (note to self: read up on shopt). Don't forget to turn it off again when you're finished with it (shopt -u extglob).
Quote:
Originally Posted by ghostdog74
Code:
# IFS="-"
# a="C9-1.4-1.6"
set -- $a
# echo $1
C9
This is also pretty cool, but you should be aware that it also unsets all of the previous positional parameters, as well as replacing the first three. This could have rather undesirable effects inside a script if you're not careful.
In the end though, it's seems obvious that there's no way to do it in a single line. It's going to take two or more commands to extract the middle portion no matter what you try. I think ntubski's solution above is the cleanest one, personally, if you really need it all done in one operation. Otherwise, the simplest solution is probably just to step it through two iterations of variable expansion.
Code:
var="C9-1.4-1.6"
var="${var#*-}"
echo "${var%-*}"
Last edited by David the H.; 07-17-2009 at 03:48 AM.
This is also pretty cool, but you should be aware that it also unsets all of the previous positional parameters, as well as replacing the first three. This could have rather undesirable effects inside a script if you're not careful.
You missed my point. It's not IFS that concerns me, but the $1, $2, etc. positional parameters. If any subsequent code in your script relies on the original input from higher-numbered positionals (there's a line that uses $4, for example), then using your line above will break it, because "set --" will unset them. And of course the original values of $1, $2, and $3 are all overwritten.
It's not necessarily a fatal flaw, but it could cause problems if you're not careful.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.