Answers:
有很多工具可以做到这一点。
当您使用时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我会使用参数扩展(如果外壳支持的话):
$ name='a.b.c.txt'
$ string1="${name%.*}"
$ string2="${name##*.}"
$ echo "$string1"
a.b.c
$ echo "$string2"
txtecho "a.b.c.txt" | cut -d. -f1-3cut命令将划定.并给你4个因素(a,b,c,txt)。上面的命令将打印因子1至3(包括)。
要么:
echo "a.b.c.txt" | cut -d -f-3上面的命令将打印因子1至3(包括在内)。