Brian K. White said the following on 07/15/2013 12:00 PM:
% F=/path/to/foo.blah.txt % echo "${F##*.}" txt
and for that matter $ F=/usr/bin/foo.x.y.z.txt $ echo "${F##*.}" txt
You are saying you want the final answer to _include_ the dot? That is an unreasonably complicated thing to try to do for not enough gain. It's possible with sed, but you're working too hard to figure out that sed/regex syntax, and by forking a process to run sed, also making the computer work too hard.
The simple and practical answer is just to add the dot back in yourself like this:
ext=".${FILENAME##*.}"
LOL!
That is not technically truth right there. If the file has no extension then you will get ext="." which basically claims that the original file name ended with a dot, which is not true.
Ah,actually $ F=/usr/bin/fooxyztxt $ echo ."${F##*.}" ./usr/bin/fooxyztxt
But since you are probably just looking for a match from a list of known extensions and they all have a leading dot, this should be fine.
Now the important part, sometimes known as Context is Everything
How to code a particular thing vastly depends on the rest of the job it is a part of. You can make your life unnecessarily hard by trying to get a detail to work a certain way without considering the rest of the context. In this case, I don't think your really need exactly what you asked for and above is good enough, except for two more things:
1 - You probably want to convert ext to all upper or all lower so that you find the matches that YOU and I know are correct even if the computer doesn't. You know that file.Avi is an avi file even though it doesn't match either .avi or .AVI.
For that you can typeset ext so it is all up or all low. put this near the top of the script, before ext is ever used. typeset -l ext or typeset -u ext
2 - You probably want to try using file magic (the "file" command) as a fall-back in case $ext was not recognized. Or even other more specialized utilities that use more in-depth knowledge of common media files to identify files by their contents instead of relying on their file names.
Take one of your video files and rename it without the extension. now run file "movie title here"
If you had trouble just clipping the ext off of a filename then parsing this output in a script is probably right out, but the info is there.
Of course if you have a lot of such files to process then perhaps streaming though sed or awk would be simpler, but as Brian says, it depends on the alrger context. -- How long did the whining go on when KDE2 went on KDE3? The only universal constant is change. If a species can not adapt it goes extinct. That's the law of the universe, adapt or die. -- Billie Walsh, May 18 2013 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org