的经典方法是basename
命令:
file="text.rtf"
new=$(basename "$file" .rtf).mobi
更现代的方式避免了行使其他程序:
file="text.rtf"
new="${file%.rtf}.mobi"
如果你真的必须使用awk
,那么我想你使用:
file="text.rtf"
new=$(echo "$file" | awk '/\.rtf$/ { sub(/\.rtf$/, ".mobi"); } { print }')
对于sed
,哟u使用:
file="text.rtf"
new=$(echo "$file" | sed 's/\.rtf$/.mobi/')
正则表达式的一个很好的解释,那么你要弗里德尔的"Mastering Regular Expressions"书。
要text.rtf.mobi
转换为text.mobi
,你可以使用任何先前细微调整显示的工具:
new=$(basename "$file" .rtf.mobi).mobi
new="${file%.rtf.mobi}.mobi"
new=$(echo "$file" | awk '/\.rtf\.mobi$/ { sub(/\.rtf\.mobi$/, ".mobi"); } { print }')
new=$(echo "$file" | sed 's/\.rtf\.mobi$/.mobi/')
,事情仅略有不同,如果.rtf
可以是任何其他扩展名,但你开始问自己:“为什么不在转换文件之前从文件中删除原始扩展名,或者使用转换器中的文件命名工具来获取所需的输出名称?”
与basename
不再有明智的做法。
new="${file/.[!.]*.mobi/}" # bash
new=$(echo "$file" | awk '/\.[^.]+\.mobi$/ { sub(\.[^.]*\.mobi$/, ".mobi"); } { print }')
new=$(echo "$file" | sed 's/\.[^.]*\.mobi$/.mobi/')
将'important.document.v1.rtf.mobi'损坏到'important.mobi'中。 –
是的我认为这是他的意思 –
是的!这个!非常感谢! – Kristian