LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2005, 01:36 PM   #1
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Rep: Reputation: 18
how to find an exact substring match?


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...

Thanks,
Lieven
 
Old 02-21-2005, 02:09 PM   #2
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
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.
 
Old 02-21-2005, 02:36 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: how to find an exact substring match?

Quote:
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".
 
Old 02-21-2005, 02:52 PM   #4
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
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?
 
Old 02-21-2005, 04:13 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
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?
Yes.
 
Old 02-21-2005, 04:32 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
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:
Code:
awk "BEGIN {print index(\"$fname\", \".wav\")}"

Last edited by Hko; 02-21-2005 at 05:16 PM.
 
Old 02-22-2005, 01:34 AM   #7
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
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.

kind regards,
Lieven
 
Old 02-22-2005, 06:28 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Yes, programming laguages generally do have some way to do that. I couldn't find a real simple way for scripts though.
 
  


Reply



Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
replace a substring with another string in C zeppelin Programming 21 11-09-2009 09:59 PM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM
C++ STL count() number of substring occurences... nyk Programming 4 06-25-2004 07:12 AM
DHCP vendor-encapsulated-options substring bwilliam79 Linux - Networking 1 10-29-2002 10:08 AM
perl: substring extraction after specific char markus1982 Programming 2 10-06-2002 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration