我有一个文件名a.b.c.txt,我希望这个字符串被拆分为
string1=a.b.c
string2=txt
Run Code Online (Sandbox Code Playgroud)
基本上我想拆分文件名及其扩展名。我使用过,cut但它拆分为a,b,c和txt。我想在最后一个分隔符上剪切字符串。
有人可以帮忙吗?
小智 37
#For Filename
echo "a.b.c.txt" | rev | cut -d"." -f2- | rev
#For extension
echo "a.b.c.txt" | rev | cut -d"." -f1 | rev
Run Code Online (Sandbox Code Playgroud)
hee*_*ayl 17
有很多工具可以做到这一点。
正如您使用的那样cut:
$ string1="$(cut -d. -f1-3 <<<'a.b.c.txt')"
$ string2="$(cut -d. -f4 <<<'a.b.c.txt')"
$ echo "$string1"
a.b.c
$ echo "$string2"
txt
Run Code Online (Sandbox Code Playgroud)
我会使用参数扩展(如果外壳支持它):
$ name='a.b.c.txt'
$ string1="${name%.*}"
$ string2="${name##*.}"
$ echo "$string1"
a.b.c
$ echo "$string2"
txt
Run Code Online (Sandbox Code Playgroud)