PDA

View Full Version : [SOLVED] Bash filter help



Gen2ly
March 15th, 2008, 10:49 AM
I'm still learning regular expressions so I hope someone doesn't mind helping me. I've learned a regular expression to filter out everything from the right most hyphen: ${TEXT%-*} here's the input I'm going to be filtering.


foobar-0.1.text
foo-bar-0.1.text


The format is always name-version.ext. ${TEXT%-*} works fine for these. But sometimes these inputs are


foo-bar-0.1-r1.text

I'm learning regular expressions at a gradual pace I'd be happy if someone couldhelp.

ghostdog74
March 15th, 2008, 11:07 AM
what are ALL the possible combinations of file names you may have?

Gen2ly
March 15th, 2008, 11:32 AM
deriviations:


foobar-0.1.text
foo-bar-0.1.text
foo-bar-0.1-r1.text

I suppose there could be a


foo-bar-bar-0.1-r1.text

later on so the filter will apply to the hyphen by the version (there will always be a version number).

Trumpen
March 15th, 2008, 12:04 PM
Given that version begins with a number you can exploit this by using something like:


${text%-[0-9]*}

Gen2ly
March 15th, 2008, 02:28 PM
thanks 4 the help. that did the trick