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.
Hi,
I'm starting to learn about regular expressions and it all seems pretty nice to me but I'm missing something vital.
In my script, I want to find a substring in a certain string. It should return the position of the first character of this substring.
I tried the following:
fname="rancid_musicfile.wav"
pos=`expr index "$fname" wav$`; echo $pos => gives 2 as a position result but I'm expecting 18 as position because I want the complete substring to be taken in account, not just one of it's elements. (isn't it [wav] that matches any of the characters?)
I also tried different other things like:
pos=`expr index "$fname" "wav"$`
pos=`expr index "$fname" 'wav'$`
pos=`expr index "$fname" "wav$"`
pos=`expr index "$fname" 'wav$'`
But they all give the first occurence of the 'a' as a result.
Can someone point out the stupidity that I'm missing here? It must be something very obvious I suppose...
I found a way to remove the wav extention from the string with the following: (But I would like to know it's position also.)
fname2=`echo $fname | sed -e 's/\.wav//'`
There might be easier ways to do this but it's only for learning purposes.
Originally posted by ldp
I tried the following:
fname="rancid_musicfile.wav"
pos=`expr index "$fname" wav$`; echo $pos => gives 2 as a position result but I'm expecting 18 as position because I want the complete substring to be taken in account, not just one of it's elements. (isn't it [wav] that matches any of the characters?)
In shell wildcard matching "[wav]" means "any of the characters" (w, a or v in this case), but "expr" does not do do this for for entire strings, but only for single one characters in "wav".
So you're saying that with expr, I cannot look for the position of a substring but only for the position of a single character (or any one char from a list like "wav") in a string?
Originally posted by ldp
So you're saying that with expr, I cannot look for the position of a substring but only for the position of a single character (or any one char from a list like "wav") in a string?
Originally posted by ldp
I found a way to remove the wav extention from the string with the following: (But I would like to know it's position also.)
fname2=`echo $fname | sed -e 's/\.wav//'`
There might be easier ways to do this but it's only for learning purposes.
Nothing wrong with yours, but a few other ways to do that:
Code:
fname2=`basename $fname .wav`
# or: (bash only, but no external programs used)
fname2=${fname%.wav}
To get the position of ".wav", you can now take just the length of fname2:
Code:
pos=`expr length $fname2`
# or: (bash only, but no external programs used)
pos=${#fname2}
As far as I know there's no utility that gets a substring index at once (a bit surprising I think). Though combining the two things gets close:
Code:
# Using $(..) instead of `..` which does the same thing,
# but $(..) can be nested (one inside the other).
# This is bash-only though.
pos=$(expr length $(basename $fname .wav))
Or with some more heavy utility like "awk" you could do:
Thanks for your explanations.
Too bad there is no command/builtin or something to give the position of a substring. If I remember well, then most programming languages do have something like that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.