trim同时在linux终端中输入一个字符串begining/end

trim the begining/end of a string at the same time in linux terminal

我有这些文件是我的输入:

library.WTHCG.30678-34569-2789.txt
library.WTHCG.45789-45688-7897.txt
library.WTHCG.56788-67879-7899.txt
library.WTHCG.45678-78987-9097.txt

我是 运行 这个命令:

for i in library*; do
    echo sspace ${i#*library.} --o $i;
done

我正在使用 ${i#*library.} 从字符串的开头删除 library.。但是我需要同时删除 --o 之后的 library..txt,这样结果看起来像:

sspace WTHCG.30678-34569-2789.txt --o WTHCG.30678-34569-2789
sspace WTHCG.45789-45688-7897.txt --o WTHCG.45789-45688-7897
sspace WTHCG.56788-67879-7899.txt --o WTHCG.56788-67879-7899
sspace WTHCG.45678-78987-9097.txt --o WTHCG.45678-78987-9097

没有办法一次性进行多个参数展开;你需要使用一个临时变量。

for i in library*; do
    j=${i#*library_}
    echo sspace "$j" --o "${j%.txt}"
done

你需要两个 expansions 来实现:

for filename in library*; do
    result=${filename#library.} # remove library. from beginning
    result=${result%.txt}       # remove .txt from the end
    echo sspace "${result}" --o "${filename}"
done