Thursday, 12 September 2013

How to extract last 2 characters before the extension of a filename in bash?

How to extract last 2 characters before the extension of a filename in bash?

What i would like to accomplish is to take a file name let's say
myfileRE.txt and return the new file name of myfile.txt. The extra two
characters will always be two characters and so what i tried to do was:
${filename%??.}
and my idea was "match the 2 characters that come right before the period
and rip those characters out" ..unfortunately that just returned the
entire filename.
I ended up doing this:
${filename%??????}.txt
but that's not very friendly and there must be a cleaner way to do it. Any
advice? Maybe something with regular expressions?

No comments:

Post a Comment