gpt4 book ai didi

bash - 在不知情的情况下从文件中删除扩展名

转载 作者:行者123 更新时间:2023-11-29 08:46:03 26 4
gpt4 key购买 nike

我知道如何删除文件的扩展名,当我知道它是:

nameis=$(basename $dataset .csv)

但我想在事先不知道的情况下删除任何扩展程序,有人知道该怎么做吗?

感谢任何帮助,泰德

最佳答案

在 bash 中,您可以执行以下操作:

nameis=${dataset%.*}

...例如:

$ dataset=foo.txt
$ nameis=${dataset%.*}
$ echo $nameis
foo

此语法在 bash 手册页中描述为:

${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.

关于bash - 在不知情的情况下从文件中删除扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5365090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com