是否有一种方法只获得glob模式的扩展部分?



假设我有一个目录,内容

$ ls
1a.png
1b.xml
1c.png
2d.png
2e.xml
2f.png

这里是否有类似命令xglob的东西?

$ xglob "1*.png" #xglob is the command I seek
a
c

虽然纯sh溶液是首选,但bashfish溶液也有帮助。

您可以在正则表达式中使用捕获组来查看字符串的哪些部分与表达式的特定部分匹配:

#!/bin/bash
for f in *
do
if [[ "$f" =~ ^"1"(.*)".png"$ ]]
then
echo "$f matches, and the subsection is ${BASH_REMATCH[1]}"
fi
done

最新更新