removing extension from file without knowing it
I know how to remove the extension of a file, when I know it as:
nameis=$(basename $dataset .csv)
but I want to remove any extension without kno开发者_开发百科wing it beforehand, anyone know how to do this?
Any help appreciated, Ted
In bash you can do the following:
nameis=${dataset%.*}
... e.g.:
$ dataset=foo.txt
$ nameis=${dataset%.*}
$ echo $nameis
foo
This syntax is described in the bash man page as:
${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the "%" case) or the longest matching pattern (the "%%" case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
Now, if you want some stylish old-school regexp:
echo "foo.bar.tar.gz" | sed "s/^\(.*\)\..*$/\1/"
-> Shall return: foo.bar.tar
I will break it down: s/ Substitute ^ From the beginning \( Mark .* Everything (greedy way) \) Stop Marking (the string marked goes to buffer 1) \. until a "." (which will be the last dot, because of the greedy selection) .* select everything (this is the extension that will be discarded) $ until the end / With (substitute) \1 The buffer 1 marked above (which is the filename before the last dot(.) / End
Cristiano Savino
You could remove everything following the last dot, if any, with sed
:
nameis=$(echo $filename | sed 's/\.[^.]*$//')
But that would not work on files with double extensions such as .tar.gz
.
Something like ${dataset%.*}
might work; beware of files without extensions, though, as it will look for a dot that's not part of an extension to chop off.
nameis=${dataset%%.*} Assumes none of your basenames have a dot in it. This returns the longest possible dotless string from the left.
I would suggest instead of basename
use sed
command for that, like this:
echo "$fileName" | sed 's/\..*$//g'
精彩评论